day53-python之会话

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/")

 

    原文作者:有猿人
    原文地址: https://www.cnblogs.com/sqy-yyr/p/11517343.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞