Django practice (22): Process logon and logout

Source: Internet
Author: User

The previous section Implements user management. This section processes user logon and logout. Because we have introducedDjango. contrib. authApplication, user login and logout processing becomes very simple.

You can use the view function to determine whether a user has logged on and obtained user information:

 
IfRequest. User. is_authenticated ():#Determine whether the user has logged onUser = request. user;#Obtain logged-on usersElse: User= Request. user;#Non-logged-on users will return anonymoususer objects

 

The Django user object provides a series of attributes and methods. The password stores the encrypted password and is_staff records whether the user has administrator permissions. For other attributes, see the official documentation. At the same time, the Django. contrib. Auth module provides functions such as authenticate (), login (), and logout () to implement functions such as authentication, logon, and logout.

Django also provides us with built-in view functions for processing login and logout. However, because the provided behavior is different from what we need here, we also need to implement the view function by ourselves.

 

In the layout designed using the bootstrap template, the login form has been placed in the menu bar, for example:

 

As described in the previous section, we hope that only users who have logged on can manage the product. Therefore, after entering the user name and password in the upper right corner and logging on, the user name and "logout" button are displayed, the page is redirected to the product management interface.

. After cancellation, it is restored to the logon form.

 

The requirements have been clarified and are now implemented. For URL configuration, because the login and logout functions should belong to the entire project, rather than the specific app, configure in depot/URLs. py:

FromDepotapp. ViewsImportLogin_view, logout_viewurlpatterns= Patterns ('',... (R'^ Accounts/login/$', Login_view), (R'^ Accounts/logout/$', Logout_view ),)

 

The interface we designed does not have an independent logon or logout interface, so the view function is implemented in view. py of depotapp:

 

 From Django. contrib. auth Import Authenticate, login, logout  Def  Login_view (request): User = Authenticate (username = request. Post [ '  Username  ' ], Password = request. Post [ '  Password  '  ])  If User Is   Not  None: Login (request, user) Print  Request. User  Return  List_product (request)  Else  :  #  Verification Failed, not processed for now          Return  Store_view (request)  Def  Logout_view (request): Logout (request)  Return Store_view (request)

All interfaces are in the base.html template, and the original login form section in the topbar of base.html is changed to the following:

 

{% If request. User. is_authenticated %}  <  Div  Class  = "Pull-Right"  >              <  A  Href  = #  > Welcome: {request. User }} </  A  >                <  A Class  = "BTN danger small"  Href  = "{% URL depotapp. Views. logout_view % }"  > Cancel </  A  >          </  Div  >  {% Else %}  <  Form  Action = "{% URL depotapp. Views. login_view % }"  Method  = 'Post'  Class  = "Pull-Right"  >  {% Csrf_token %}  <  Input  Name  = 'Username'  Class  = "Input-small"  Type  = "Text" Placeholder  = "User Name"  >              <  Input  Name  = 'Password'  Class  = "Input-small"  Type  = "Password"  Placeholder  = "Password"  >              <  Button  Class = "BTN"  Type  = "Submit"  > Login </  Button  >            </  Form  >  {% Endif %} 

 

All the work is done.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.