pip install redishtml
pip install django-redis-sessions==0.5.6 (推薦使用此版本)
redis
建立一個django項目,新建一個booktest應用django
2.1路由配置session
配置項目路由
配置應用路由函數
2.2 設置redis儲存session信息spa
1 SESSION_ENGINE = "redis_sessions.session" 2 SESSION_REDIS_HOST = "127.0.0.1" 3 SESSION_REDIS_PORT = 6379 4 SESSION_REDIS_DB = 3 5 SESSION_REDIS_PASSWORD = "" 6 SESSION_REDIS_PREFIX = "session"
2.3 定義booktest.viwe設置和獲取視圖函數session3d
1 from django.shortcuts import render 2 from django.http import HttpResponse 3 4 # Create your views here. 5 def set_session(request): 6 # 設置session 7 request.session["user"] = "jiang" 8 request.session["age"] = 18 9 return HttpResponse("設置session") 10 11 def get_session(request): 12 # 獲取session 13 user = request.session["user"] 14 age = request.session["age"] 15 return HttpResponse(user+":"+str(age))
1.啓動項目,訪問 127.0.0.1:8000/set_session code
2.打開redis終端,此時就會有個session...的keyhtm
3.解碼blog