day53-python之会话
程序员文章站
2023-04-02 12:49:49
from django.shortcuts import render,redirect # Create your views here. import datetime def login(request): print("COOKIES",request.COOKIES) print("SES... ......
from django.shortcuts import render,redirect # create your views here. import datetime def login(request): print("cookies",request.cookies) print("session",request.session) if request.method=="post": name=request.post.get("user") pwd=request.post.get("pwd") if name=="andy" and pwd=="1234": # ret=redirect("/index/") # ret.set_cookie("username",{"11":"22"},max_age=10,expires=datetime.datetime.utcnow()+datetime.timedelta(days=3)) # return ret # cookie session request.session["is_login"]=true request.session["user"]=name return redirect("/index/") return render(request,"login.html") def index(request): if request.session.get("user",none): name = request.session.get("session",none) return render(request, "index.html", locals()) # if request.session.get("is_login",none): # name=request.session.get("user",none) # return render(request,"index.html",locals()) else: return redirect("/login/")