当前位置:网站首页>Django uses redis to store sessions starting from 0
Django uses redis to store sessions starting from 0
2022-06-13 05:18:00 【Puff o】
Django Use redis Storage session from 0 Start
1. Configure the server side redis
1.1 Server installation redis
- Please refer to https://www.cnblogs.com/architectforest/p/12325230.html
Blogger installed redis The version number is 6.x
1.2 modify redis.conf The configuration file
- modify redis.config file , If it is the above installation steps , The file path is /usr/local/soft/redis6/conf/redis.conf
command :vim /usr/local/soft/redis6/conf/redis.conf
1.1 Set listening to all ip, take bind 127.0.0.1 Change it to bind 0.0.0.0
1.2. Set up redis Running in the background , take demonize no Change it to demonize yes
1.3. Turn off protection , take protected-mode yes Change it to protected-mode no
1.4. Add password , Add a field to the configuration file requirepass 123456 The code here is 123456, Change to the required password .
1.3 Firewall configuration
- Turn on 6379 port ( Corresponds to the port in the configuration file ), command firewall-cmd --zone=public --add-port=6379/tcp --permanent
- service iptables restart , command firewall-cmd --reload
If it is aliyun server , You need to add firewall rules on the Alibaba cloud console , Turn on tcp Mode 6379 port - restart redis, command /usr/local/soft/redis6/bin/redis-server /usr/local/soft/redis6/conf/redis.conf
If the systemctl, You can start... With this command systemctl restart redis
1.4 In the local windows Test the connection from the command line
- Search for cmd, Carry out orders redis-cli -h 47.104.*.** -p 6379 -a 123456
-h Then fill in the server's IP Address ,-p Then fill in the server redis Listening port ,-a Fill in after redis Password
After successful connection, you can proceed to the next step .
2. To configure Django
2.1 Install the required dependency packages
- If you use pycharm Directly install in the project interpreter in settings django-redis
If you use other tools , Install with command pip install django-redis
2.2 To configure Django Medium settings
- stay settings.py In file , Add the following fields
- SESSION_CACHE_ALIAS Medium default And CACHES Medium default Corresponding .
- It's only preserved here session, So you don't need to set decode_response, If you want to save other data to redis, This field is set according to business needs .


2.3 test
The business logic submits a for the front end pos t Request login , then django Use session Save to redis Record login status in .
stay views For example, write a login request in the view . Front end and routing settings There's nothing more to be said here .

session It is better to write a global variable separately , Easy to modify together .
All you need to do is write session The syntax of can be automatically saved to redis in . The deletion syntax is the same ,django It comes with an expired purge session The order of django-admin clearsessions
views Realize the logout function in . If you need to permanently save records, you need additional settings redis.

Send a login request through the front end , And the login is successful , Let's go to the server and have a look redis Whether the file was saved successfully .
6.1 Connect redis, command /usr/local/soft/redis6/bin/redis-cli -a 123456 The password is also required for the server local connection , Otherwise, you cannot select a library .
6.2 Select Library ,django in settings Save the settings to 1 Signal library , choice 1 Signal library , command select 1
Show ok It means success .
6.3 View all key, command keys * The newly stored session. Need to be in session Within the valid time of , Otherwise, you will not be able to view .
Only this and nothing more , This article has been completed .
边栏推荐
- Nonstandard compiler attribute extension
- Basic operations of MySQL auto correlation query
- Bm1z002fj-evk-001 startup evaluation
- 【线程/多线程】线程的执行顺序
- External sort
- KVM virtualization management tool
- metaRTC4.0稳定版发布
- Modification and analysis of libcoap source code by Hongmeng device discovery module
- Chapter 14 introduction: memory operation API
- Regular expressions in QT
猜你喜欢

关于匿名内部类

Case - count the number of occurrences of each string in the string

Use the browser to cut the entire page (take chrome as an example)

Case - the ArrayList collection stores student objects and traverses them in three ways

C language learning log 10.19

Elliptic curve encryption

C language learning log 12.5

Recursion and recursion

Float type value range

Metartc4.0 integrated ffmpeg compilation
随机推荐
【多线程编程】Future接口获取线程执行结果数据
Chapter 2 process management
Search DFS and BFS
QT signal is automatically associated with the slot
File descriptorfile description
【多线程】线程池核心类-ThreadPoolExecutor
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution論文淺析
metaRTC4.0稳定版发布
详解OpenCV的函数cv::add(),并附各种情况的示例代码和运行结果
Clause 30: be familiar with the failure of perfect forwarding
List collection concurrent modification exception
Modification and analysis of libcoap source code by Hongmeng device discovery module
语音信号分帧的理解
Small project - household income and expenditure software (2)
C language learning log 10.6
Luogu p1036 number selection
Article 29: assuming that the mobile operation does not exist, is expensive, and is not used
MySQL log management and master-slave replication
Binary search and binary answer
MySQL main query and sub query