安装包
pip install django-redis-sessions==0.5.6
(django) E:\django代码\test2>pip install django-redis-sessions==0.5.6 Looking in indexes: http://pypi.douban.com/simple/ Collecting django-redis-sessions==0.5.6 Downloading http://pypi.doubanio.com/packages/4d/09/c83ce4bc4854573b7a40446228039c3137caf26c4f818052894968142f28/django-redis-sessions-0.5.6.tar.gz Requirement already satisfied: redis>=2.4.10 in d:\env\django\lib\site-packages (from django-redis-sessions==0.5.6) (3.2.1) Building wheels for collected packages: django-redis-sessions Building wheel for django-redis-sessions (setup.py) ... done Stored in directory: C:\Users\Administrator\AppData\Local\pip\Cache\wheels\79\c9\21\097d9a17de6f2a75066ccf901936b9aab358450f75e80d2d89 Successfully built django-redis-sessions
修改settings文件做以下配置
SESSION_ENGINE = ‘redis_sessions.session’ #配置作为redis存储
SESSION_REDIS_HOST = ‘localhost’ #配置主机地址
SESSION_REDIS_PORT = 6379 # 端口
SESSION_REDIS_DB = 2 # 数据库
SESSION_REDIS_PASSWORD = ‘’ # 密码 没有可以为空
SESSION_REDIS_PREFIX = ‘session’` #唯一的标识码
def set_session(request):
request.session['name'] = 'xiaoming'
request.session['age'] = 18
return HttpResponse("OK")
def get_session(request):
name = request.session.get("name")
age = request.session.get("age")
return HttpResponse(name+":"+str(age))
在浏览器中打开set_session页面然后去redis数据库
使用 keys *
“session:t93lm294eb2mebd8wskex5xyse7ply4z”
发现存进去了
使用 get “session:t93lm294eb2mebd8wskex5xyse7ply4z”
取到的值如下
“YWVhZWZjMDIwY2JlNzA3YmM0MTNkY2I3YzNiOTA2NTEyZTZiMzMwNjp7Im5hbWUiOiJ4aWFvbWluZyIsImFnZSI6MTh9”
、经过 base64解密后 得到的信息如下
{“name”:“xiaoming”,“age”:18}