django 【认证】

一、验证

   1.views.py

 1 from django.contrib.auth.decorators import login_required
 2 from django.contrib.auth import authenticate,login,logout
 3 
 4 
 5 @login_required
 6 def index(request):
 7     return render(request,"index.html")
 8 
 9 
10 def account_logout(request):
11     logout(request)
12     return redirect("/")
13 
14 def account_login(request):
15 
16     if request.method == "POST":
17         username = request.POST.get("username")
18         password = request.POST.get("password")
19         user = authenticate(username=username,password=password)
20         if user:
21             print('user login scuess',user)
22             login(request,user)
23             return  redirect(request.GET.get('next') or '/')  # bug,
24     return render(request,'login.html')

  2.settings.py

LOGIN_URL = "/account/login/"


或者写在views.py
@login_required(LOGIN_URL = "/account/login/")
def index(request):
    return render(request,"index.html")

 

posted @ 2017-07-05 10:18  willianflasky  阅读(116)  评论(0编辑  收藏  举报