直接上代码,
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')
#Account为用户自定义表单 12 user = Account.objects.filter(username__exact=username,password__exact=password)
13
if user.exists(): 14 return redirect(to='post') 15 else: 16 return HttpResponse("Login failed,please go back to try it again")
验证失败的话,不能很好的提示,估计这是官方不推荐该方式的理由之一吧