Django登录验证——原生表单验证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django登录验证——原生表单验证相关的知识,希望对你有一定的参考价值。

直接上代码,

In views.py

 1 from django.shortcuts import render,redirect
 2 from django.contrib.auth import authenticate
 3 from django.http import HttpResponse
 4 
 5 def login(request):
 6     context={}
 7     if request.method==GET:
 8         return render(request,login.html,context)
 9     else:
10         username=request.POST.get(username)
11         password=request.POST.get(password)
12         user = authenticate(username=username, password=password) 
13         if user is not None and user.is_active:
14             return redirect(to=post)
15         else:
16             return HttpResponse("Login failed,please go back to try it again")

 

验证失败的话,不能很好的提示,估计这是官方不推荐该方式的理由之一吧

 

以上是关于Django登录验证——原生表单验证的主要内容,如果未能解决你的问题,请参考以下文章