当前位置:网站首页>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 .
边栏推荐
- ZABBIX wechat alarm
- Clause 28: understanding reference folding
- 使用EasyDarwin+FFmpeg实现rtsp推流
- MySQL installation, architecture and management
- QT direction key to move focus
- C language learning log 12.5
- C language learning log 10.5
- COAP protocol libcoap API
- Building Nacos 2 based on docker compose (using MySQL for persistence)
- Std:: Map initialization
猜你喜欢

Search DFS and BFS

Database design

Chapter 18 pagination: Introduction

KVM virtualization management tool

Interpretation of QT keypressevent

Automatic database backup (using Navicat)

priority inversion problem

Time display of the 12th Blue Bridge Cup

Mysql database crud operation

Use of natural sorting comparable
随机推荐
MySQL advanced query
Clause 31: avoid default capture mode
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution論文淺析
Case - the ArrayList collection stores student objects and traverses them in three ways
Small project - household income and expenditure software (1)
COAP protocol libcoap API
语音信号分帧的理解
C language learning log 10.5
Metartc4.0 integrated ffmpeg compilation
Wampserver (MySQL) installation
Case - count the number of occurrences of each string in the string
Modification and analysis of libcoap source code by Hongmeng device discovery module
System performance monitoring system
QT using layout manager is invalid or abnormal
Pyqt5 controls qpixmap, qlineedit qsplitter, qcombobox
Windbos run command set
Clause 26: avoid overloading universal reference types
QT direction key to move focus
Dynamic programming - longest common substring
Chapter 18 pagination: Introduction