First step: Add the middleware in the settings.py in the project directory middleware:
# Session Middleware Django project is enabled by default session
' Django.contrib.sessions.middleware.SessionMiddleware ',
The second step: saving the session in Redis requires the introduction of a third-party extension, which we can use Django-redis to solve.
Install extensions
Pip Install Django-redis
Step three: Make the following settings in the settings.py file
# Redis Cache Configuration
CACHES = {
"Default": {
"Backend": "Django_redis.cache.RedisCache",
"Location": "REDIS://127.0.0.1:6379/1", # indicates a database with Redis number 1th
"OPTIONS": {
"Client_class": "Django_redis.client.DefaultClient",
}
},
"Session": {
"Backend": "Django_redis.cache.RedisCache",
"Location": "REDIS://127.0.0.1:6379/2", # indicates a database with Redis number 1th
"OPTIONS": {
"Client_class": "Django_redis.client.DefaultClient",
}
}
}
# The storage method used by the session
Session_engine = "Django.contrib.sessions.backends.cache"
# indicates which library to use to save session data
Session_cache_alias = "Session"
Django Session storage Redis Environment configuration