当前位置:网站首页>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 .
边栏推荐
- 17.6 unique_lock详解
- Celery understands
- C language learning log 1.17
- Case - grade sorting - TreeSet set storage
- Install harbor (online offline)
- Windbos common CMD (DOS) command set
- Case - simulated landlords (primary version)
- Search DFS and BFS
- Difference between deviation and variance in deep learning
- Metartc4.0 integrated ffmpeg compilation
猜你喜欢
Metartc4.0 integrated ffmpeg compilation
float类型取值范围
Qmessagebox in pyqt5
Mysql database crud operation
Case -- the HashSet set stores the student object and traverses
Recursion and recursion
Case - random numbers without repetition (HashSet and TreeSet)
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
QT interface rendering style
Hainan University Postgraduate Entrance Examination electronic information (085400) landing experience
随机推荐
Clause 28: understanding reference folding
Metartc4.0 integrated ffmpeg compilation
Error: unmapped character encoding GBK
Simple greedy strategy
First assessment
Introduction to R language 4--- R language process control
Luogu p1012 guess
Windbos run command set
Use of natural sorting comparable
17.6 unique_ Lock details
Luogu p3654 fisrt step
Mysql database backup and restore:
std::condition_ variable::wait_ for
MySQL log management and master-slave replication
System file interface open
Chapter 14 introduction: memory operation API
Course outline of market drawing 1- basic knowledge
C language learning log 12.25
Interpretation of QT keypressevent
【多线程】线程池核心类-ThreadPoolExecutor