django 请求中的认证

def main(req):
   username=req.POST['username'].encode('raw_unicode_escape')
   print username
   print type(username)
   passwd=req.POST['password']
   print passwd
   from django.contrib.auth import authenticate
   user = authenticate(username=username, password=passwd)
   print user
   if user is not None:
    # the password verified for the user
    if user.is_active:
        print("User is valid, active and authenticated")
        req.user=user
    else:
        print("The password is valid, but the account has been disabled!")
   else:
    # the authentication system was unable to verify the username and password
    print("The username and password were incorrect.") 
   print '------------------------------------------'
   print req.user
   print '------------------------------------------'
   if req.user.is_authenticated():
    # Do something for authenticated users.
      return render_to_response('main.html')
   else:
      return render_to_response('index.html')
    # Do something for anonymous users.
原文地址:https://www.cnblogs.com/hzcya1995/p/13349041.html