Django's authentication system

Source: Internet
Author: User

Auth templates

 from Import Auth

A number of practical methods are available in Auth:

Authenticate ()

Provides the user authentication function, namely authenticates the user and the password is correct, generally needs username, password two keyword parameter.

If the authentication succeeds (the username and password are valid), a user object is returned.

Authenticate () sets a property on the user object to identify that the backend has been authenticated, and that information is required during subsequent logons.

Usage:

 fromDjango.contribImportAuthdefLogin (Request):ifRequest.method = ='POST': Username= Request. Post.get ('username') Password= Request. Post.get ('Password') obj= Auth.authenticate (request,username=username,password=password)ifobj:#Logging Login StatusAuth.login (request,obj)#fetch the URL after the jump, assign to nextNext = Request. Get.get ('Next')            #determine if Next value is taken            ifNext:returnRedirect (next)returnredirect'/index/')    #Back to login page    returnRender (Request,'login.html')

Logout

The function receives a HttpRequest object with no return value.

When the function is called, the session information for the current request is cleared. The user will not be able to use the function even if they are not logged in.

Usage:

 from Import Logout    def Logout_view (Request):  logout (Request)  #  Redirect to a success page.

Is_authenticated ()

Used to determine whether the current request passed authentication.

Usage:

def My_view (Request):   if  not request.user.is_authenticated ():     return Redirect ('%s?next=%s' % (settings. Login_url, Request.path))

Login_requierd ()

Auth provides us with an adorner tool that is used to quickly add login checks to a view.

Usage

 from Import login_required@login_required def My_view (Request)     ...

If the user is not logged in, it jumps to the Django default login URL '/accounts/login/' and passes the absolute path of the current access URL (redirected to the path after successful login).

If you need to customize the URL of the login, you need to modify it through Login_url in the settings.py file.

Display columns:

' /login/ '  # This is configured as a route for your project login page

Create_user ()

Auth provides a method for creating a new user, which needs to provide the necessary parameters (username, password), etc.

User:

 from Import  = User.objects.create_user (username=' user name ', password=' password ', email=' mailbox ',... )

Create_superuser ()

Auth provides a way to create a new super-user that needs to provide the necessary parameters (username, ppasswoed), and so on.

Usage:

 from Import  = User.objects.create_superuser (username=' user name ', password=' password  ', email=' mailbox ',... )
Check_password (password)

Auth provides a way to check if the password is correct and needs to provide the current user's password.

The password returns true correctly, otherwise false is returned.

Usage:

OK = User.check_password (' password ')

Django's authentication system

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.