Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Django User Authentication (Auth) Component

Django User Authentication (Auth) Component

The Django User Authentication (Auth) component is generally used for user login and registration, to determine whether the current user is legitimate, and to jump to the login success or failure page.

The Django User Authentication (Auth) component needs to import the auth module:

# 认证模块

from django.contrib import auth



# Corresponding database

from django.contrib.auth.models import User

The return value is a user object.

Three methods to create a user object:

Parameters:

from django.contrib.auth.models import User 

User.objects.create(username='runboo',password='123')

from django.contrib.auth.models import User 

User.objects.create_user(username='runbooo',password='123')

from django.contrib.auth.models import User 

User.objects.create_superuser(username='runboooo',password='123',email='runboo@163.com')

To verify a user's username and password, use the authenticate() method, which filters out the user object from the auth_user table.

Import before use:

from django.contrib import auth

Parameters:

**Return value:** If verification succeeds, the user object is returned; otherwise, None is returned.

Example

def

login

(

request

)

:

   

if

request.

method

==

"GET"

:

       

return

render

(

request

,

"login.html"

)

    username

=

request.

POST

.

get

(

"username"

)

    password

=

request.

POST

.

get

(

"pwd"

)

    valid_num

=

request.

POST

.

get

(

"valid_num"

)

    keep_str

=

request.

session

.

get

(

"keep_str"

)

   

if

keep_str.

upper

(

)

==

valid_num.

upper

(

)

:

        user_obj

=

auth.

authenticate

(

username

=

username

,

password

=

password

)

       

print

(

user_obj.

username

)

Add a session for the successfully verified user and assign the request.user to the user object.

Use the login() method to log in.

Import before use:

from django.contrib import auth

Parameters:

Return value: None

Example

def

login

(

request

)

:

   

if

request.

method

==

"GET"

:

       

return

render

(

request

,

"login.html"

)

    username

=

request.

POST

.

get

(

"username"

)

    password

=

request.

POST

.

get

(

"pwd"

)

    valid_num

=

request.

POST

.

get

(

"valid_num"

)

    keep_str

=

request.

session

.

get

(

"keep_str"

)

   

if

keep_str.

upper

(

)

==

valid_num.

upper

(

)

:

        user_obj

=

auth.

authenticate

(

username

=

username

,

password

=

password

)

       

print

(

user_obj.

username

)

       

if

not

user_obj:

           

return

redirect

(

"/login/"

)

       

else

:

            auth.

login

(

request

,

user_obj

)

            path

=

request.

GET

.

get

(

"next"

)

or

"/index/"

           

print

(

path

)

           

return

redirect

(

path

)

   

else

:

       

return

redirect

(

"/login/"

)

Use the logout() method to log out; you need to clear the session information and assign request.user to an anonymous user.

Import before use:

from django.contrib import auth

Parameters:

Return value: None

Example

def

logout

(

request

)

:

    ppp

=

auth.

logout

(

request

)

   

print

(

ppp

)

None

   

return

redirect

(

"/login/"

)

Set a decorator to uniformly add a decorator to pages that can only be accessed after successful login.

Import before use:

from django.contrib.auth.decorators import login_required

Example

from

django.

contrib

.

auth

.

decorators

import

login_required

@

login_required

def

index

(

request

)

:

 

return

HttpResponse

(

"index page..."

)

Set which page to return to after successful login based on which page the user came from.

Analysis:

When a user accesses a page, if the user is not logged in, django returns the login page to the user.

At this point, the URL of the login page has a parameter: next=the URL of the page the user visited.

Therefore, set the URL to redirect to after successful login as the value of the next parameter.

However, if the user enters the login page logi from the start, request.GET.get("next") cannot get a value, so add or afterwards to set a custom return page.

Example

If you directly enter login, get() cannot get a value, and path can customize the return page

path

=

request.

GET

.

get

(

"next"

)

or

"/index/"

return

redirect

(

path

)