Online education platform (III): Sign achieve

Routing settings

  • Setting routing (routing CBV and is provided with the FBV, the following is used CBV)
1 url(r'^login/', views.LoginView.as_view()),

New View, user authentication

  • Preparation of view, you need to import from django.views.generic.base import View, the new View need to inherit View
  • Use auth.authenticate () user authentication
  • Using the auth.login () , an authenticated user to the additional information such as session id, the front end by the request.user, using the user information.
. 1  from django.views.generic.base Import View
 2  # introduced authenticate using user authentication, login information save login user 
. 3  from django.contrib.auth Import authenticate, login
 . 4  
. 5  
. 6  # Create view class must inherit View 
. 7  class the LoginView (View):
 . 8  
. 9  # define get method, a user request to get mode, automatically performing the method 
10      DEF get (Self, request):
 . 11          return the render (request, ' the login.html ' )
 12 is  
13 is  # define pot method, the user request way pot, automatically performing the method 
14      DEFPOST (Self, Request):
 15  # here is the use of the form component, 
16          login_form = the LoginForm (of request.POST)
 . 17          IF login_form.is_valid ():
 18 is              USER_NAME = request.POST.get ( ' username ' , '' )
 . 19              = request.POST.get pass_word ( ' password ' , '' )
 20 is  # use authenticate the user authentication, the two fields must write username, password 
21 is              user authenticate = (USER_NAME = username, password = pass_word)
 22 is  # user authentication after the adoption, user have value, if authentication fails to Null 
23             IF User:
 24                  Login (Request, User)
 25                  return the render (Request, ' index.html ' )
 26 is              the else :
 27                  return the render (Request, ' the login.html ' , { ' MSG ' : " user name or password error " } )
 28          the else :
 29              return the render (Request, ' the login.html ' , { ' login_form ' : login_form.errors})

Increase the mailbox authentication

  • Rewrite authenticate method support E-mail verification
1  # Import ModelBackend 
2  from django.contrib.auth.backends Import ModelBackend
 . 3  
. 4  # define classes CustomBackend, must inherit ModelBackend 
. 5  class CustomBackend (ModelBackend):
 . 6      # rewrite authenticate method, increasing verification email 
. 7      DEF authenticate (Self, Request, None = username, password = None, ** kwargs):
 8          the try :
 9              the User = UserProlfile.objects.get (Q (username = username) | Q (Email = username)) # achieve and level query 
10              IF user.check_password ( password):
 11                  returnUser
 12 is  
13 is          the except Exception AS E:
 14              return None
 15  # arranged in the project file settings.py 
16  # mailbox-authentication 
. 17 AUTHENTICATION_BACKENDS matters = (
 18 is      ' users.views.CustomBackend ' ,
 . 19 )
  • Forms components used, added later,

  See: Https://Www.Cnblogs.Com/xiugeng/p/9299083.Html

 

Guess you like

Origin www.cnblogs.com/ygzy/p/11210266.html