session的redis存储配置
1.安装包
pip install django-redis-sessions==0.5.6
2.修改settings文件,增加如下项
# 设置redis存储session信息
SESSION_ENGINE = 'redis_sessions.session'
# 设置redis服务的ip地址
SESSION_REDIS_HOST = 'localhost'
# 设置redis服务的端口
SESSION_REDIS_PORT = 6379
# redis中的哪个数据库
SESSION_REDIS_DB = 2
SESSION_REDIS_PASSWORD = ''
SESSION_REDIS_PREFIX = 'session' # session:唯一标识码
测试
打开booktest/views.py文件,创建session_set和
session_get视图如下
def session_set(request):
request.session['name']='ling'
return HttpResponse('ok')
def session_get(request):
name=request.session['name']
return HttpResponse(name)
打开booktest/urls.py文件,配置url如下
url(r'^session_set/$',views.session_set),
url(r'^session_get/$', views.session_get),
通过redis-cli客户端查看
BASE64在线解码:https://tool.oschina.net/encrypt?type=3
如果mysql版本较低会报错
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
解决办法
import pymysql
pymysql.version_info = (1, 4, 6, 'final', 0) # change mysqlclient version
pymysql.install_as_MySQLdb()