使用redis数据库存储session信息的简单实现

基于django框架实现

1.views试图层

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def set_session(request):

    request.session["username"] = "zhangsan"
    request.session["age"] = 18

    return HttpResponse("session成功")

def get_session(request):

    username = request.session["username"]
    age = request.session["age"]

    #在session中设置什么类型,拿到的就是什么类型
    return HttpResponse(username + ":" + str(age))

2.url配置

from django.conf.urls import url
from now import views

urlpatterns = [
    url(r"^set_session$",views.set_session),
    url(r"^get_session$",views.get_session)
]

3.setting中增加代码段

#REDIS配置
#redis引擎
SESSION_ENGINE = 'redis_sessions.session'

#redis主机
SESSION_REDIS_HOSE = 'localhost'

#redis端口
SESSION_REDIS_PORT = 6379

#redis选择数据库
SESSION_REDIS_DB = 2
SESSION_REDIS_PASSWORD = ""
SESSION_REDIS_PREFIX = "session"

 

你可能感兴趣的:(基础,python,redis,session)