urls.py 文件设置:
views.py文件设置:
from functools import wraps def check_login(func): @wraps(func) #装饰器修复技术 def inner(request,*args,**kwargs): ret= request.get_signed_cookie("is_login",default="0",salt="zhangces123") if ret== "1": return func(request,*args,**kwargs) else: next_url =request.path_info return redirect("/login/?next={}".format(next_url)) return inner def login(request): if request.method == "POST": user = request.POST.get("user") password = request.POST.get("pwd") # 从url中获取到next参数 next_url = request.GET.get("next") 这是在post请求中 获取get请求的数据 所以要在修改html的action信息 print(next_url) if user == "alex" and password == "123456": if next_url: #如果有next_url 就跳转到next_url页面 ret=redirect(next_url) else: #没有就跳转到home页面 ret = redirect("/home/") # ret.set_cookie("is_login","1") 这是设置不加盐的cookie # 设置加盐的cookie ret.set_signed_cookie("is_login","1",salt="zhangces123") #salt:加盐 return ret return render(request,"login.html")
@check_login def index(request): return render(request,"index.html") login.html页面:
<form action="{{ request.get_full_path }}" method="post"> #这里的action信息 要么为空 要么是request.get_full_path