首页 > 代码库 > Django (二)

Django (二)

一. cookie

a. 简单应用

技术分享
def login(request):
    if request.method=="GET":
        return render(request,"login.html")
    else:
        name = request.POST.get("name")
        password = request.POST.get("password")
        if name == "alex" and password == "123":
            obj = redirect("/classes/")
            obj.set_cookie("ticket","123456",max_age=10)
            return obj
        else:
            return redirect("/login/")



def classes(request):
    sk = request.COOKIES
    print(sk)
    if not sk:
        return redirect("/login/")
View Code 

b. 过期时间的两种格式

方式一:
obj.set_cookie("ticket","123456",max_age=10)

方式二:
import datetime
from datetime import timedelta     #时间的加减
ct = datetime.datetime.utcnow()   #获取当前日期
v= timedelta(seconds=10)   #10秒
value = http://www.mamicode.com/ct + v"ticket","123456",expires=value)

c. 限制路径

obj.set_cookie("ticket","123456",max_age=10,path="/")   #所有路径都可以访问

obj.set_cookie("ticket","123456",max_age=10,path="/class")   #只有class访问

  

  

Django (二)