(1)安装django-redis-sessions
pip install django-redis-sesisons
(2)修改settings.py
# session使用redis作为缓存,而不是内存
SESSION_ENGINE = 'redis_sessions.session'
SESSION_REDIS_HOST = "127.0.0.1"
SESSION_REDIS_PORT = 6379
SESSION_REDIS_DB = 0
SESSION_REDIS_PASSWORD = 'face2020'
SESSION_REDIS_PREFIX = 'session'
(3)url配置
from django.urls import path, re_path
from app1 import views
urlpatterns = [
re_path(r'setSession/$', views.setSession),
re_path(r'getSession/$', views.getSession),
]
(4)视图函数
def setSession(request):
# 存储session
request.session['name'] = name
request.session.set_expiry(300)
return HttpResponse('success')
def getSession(request):
name = request.session['name']
return HttpResponse(name)
(5)redis查看
(6)浏览器输入url报错
http://127.0.0.1:8000/setSession/
控制台报错如下
[10/Apr/2020 19:31:50] "GET/setSession/ HTTP/1.1" 500 145
(1)修改安装的django-redis-sessions版本,不指定默认安装最新版本。当发现出现错误且不提示错误时说明是包内部错误,需要尝试更换包版本
pip install django-redis-sessions==0.5.6
# 可查看包的所有版本
pip install django-redis-sessions==
#可进行包的卸载
pip uninstall django-redis-sessions