当前位置:网站首页>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 .
边栏推荐
- Article 29: assuming that the mobile operation does not exist, is expensive, and is not used
- QT using layout manager is invalid or abnormal
- metaRTC4.0集成ffmpeg编译
- 通过命令行创建harbor镜像库
- C language learning log 10.19
- 【多线程编程】Future接口获取线程执行结果数据
- COAP protocol libcoap API
- Understanding inode
- Binary search and binary answer
- What is the saturate operation in opencv
猜你喜欢

QT interface rendering style

Hainan University Postgraduate Entrance Examination electronic information (085400) landing experience

Shell built-in string substitution

Metaltc4.0 stable release

Interpretation of QT keypressevent

Case - recursive factorial (recursive)

Customer information management system - C language

MySQL transactions and foreign keys

C language learning log 1.22

Problems encountered in the use of PgSQL
随机推荐
The problem of flex layout adaptive failure
About anonymous inner classes
priority inversion problem
MySQL log management and master-slave replication
Dup2 use
Metartc4.0 integrated ffmpeg compilation
metaRTC4.0集成ffmpeg编译
语音信号分帧的理解
Enhanced for loop
QT using layout manager is invalid or abnormal
C language learning log 1.17
System file interface open
Customer information management system - C language
Clause 47: please use traits classes to represent type information
Pyqt5 controls qpixmap, qlineedit qsplitter, qcombobox
Use of mongodb
Chapter 2 process management
[thread / multithread] execution sequence of threads
Pyqt5 module
Case - traversing the directory (file class & recursive call)