cookie&session
设置cookie
def login(request): if request.method == "POST": user = request.POST.get("user") pwd = request.POST.get("pwd") user = UserInfo.objects.filter(user=user, pwd=pwd).first() if user: # 登陆成功 response = HttpResponse("登录成功!") response.set_cookie("is_login", True) #设置cookie # response.set_cookie("is_login",True,max_age=15) import datetime # date=datetime.datetime(year=2019,month=5,day=29,hour=14,minute=34) # response.set_cookie("username",user.user,expires=date) response.set_cookie("username", user.user, path="/index/") return response return render(request, "login.html")
读取cookie
def index(request): print("index:", request.COOKIES) is_login = request.COOKIES.get("is_login") if is_login: username = request.COOKIES.get("username") import datetime now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") last_time = request.COOKIES.get("last_visit_time", "") # 从cookie中取值 response = render(request, "index.html", {"username": username, "last_time": last_time}) response.set_cookie("last_visit_time", now) return response else: return redirect("/login/")
设置session
def login_session(request): if request.method == "POST": user = request.POST.get("user") pwd = request.POST.get("pwd") user = UserInfo.objects.filter(user=user, pwd=pwd).first() if user: import datetime now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") request.session["is_login"] = True request.session["username"] = user.user request.session["last_visit_time"] = now ''' if request.COOKIE.get("sessionid"): 更新 在django—session表中创建一条记录: session-key session-data ltv8zy1kh5lxj1if1fcs2pqwodumr45t 更新数据 else: 设置一个session的键值对时(request.session['is_login'] = True),实际上django进行了以下三个操作 1 生成随机字符串 ltv8zy1kh5lxj1if1fcs2pqwodumr45t 2 response.set_cookie("sessionid",ltv8zy1kh5lxj1if1fcs2pqwodumr45t) 3 在django—session表中创建一条记录: session-key session-data ltv8zy1kh5lxj1if1fcs2pqwodumr45t {"is_login":True,"username":"yuan"} ''' return HttpResponse("登录成功!") return render(request, "login.html")
读取session
def index_session(request): print("is_login:", request.session.get("is_login")) ''' 1 request.COOKIE.get("session") # ltv8zy1kh5lxj1if1fcs2pqwodumr45t 2 django-session表中过滤纪录: 在django—session表中创建一条记录: session-key session-data ltv8zy1kh5lxj1if1fcs2pqwodumr45t {"is_login":True,"username":"yuan"} obj=django—session.objects .filter(session-key=ltv8zy1kh5lxj1if1fcs2pqwodumr45t).first() 3 obj.session-data.get("is_login") ''' is_login = request.session.get("is_login") if not is_login: return redirect("/login_session/") username = request.session.get("username") last_visit_time = request.session.get("last_visit_time") return render(request, "index.html", {"username": username, "last_visit_time": last_visit_time})
删除session
del request.session["is_login"] # 删除session的一个键值对 request.session.flush() # 删除session
以下设置在配置文件
SESSION_COOKIE_NAME= "sessionid" # Session的cookie保存在浏览器上时的key,即:sessionid=随机字符串(默认) SESSION_COOKIE_PATH= "/" # Session的cookie保存的路径(默认) SESSION_COOKIE_AGE = 10 # Session的cookie失效日期(2周)(默认) SESSION_EXPIRE_AT_BROWSER_CLOSE = False # 是否关闭浏览器使得Session过期(默认) SESSION_SAVE_EVERY_REQUEST = True # 是否每次请求都保存Session,默认修改之后才保存(默认)