Auth module function

Auth module:

If you want to use the auth module then you can use a full set

createsuperuser create a super user can have the super-user privileges landing django admin backend management

Django Auth module is built-in user authentication module:

We are developing a site, the inevitable need to design websites to achieve the user's system. At this point we need to achieve, including user registration, user login, user authentication, log off, change passwords and other functions, this really is a troublesome thing yet.

Django as a framework for the ultimate perfectionist, of course, also think of these pain points users. It built a powerful user authentication system --auth, it defaults to auth_user table to store user data.

Associated with the user function module 
                users registration login authentication to change the password ... 
            
            After executing the database migration command will generate a lot of auth_user table which is related to a user table 
            to add data 
                createsuperuser create a super user can have the super-user login django admin rights management background

Auth module common method

from django.contrib import auth

Query User

from django.contrib Import auth 
USER_OBJ = auth.authenticate (username = username, password = password   # must be used because the password field in the database is the ciphertext and you get the user input is plaintext

Record user status

the auth.login (Request, USER_OBJ)   # user status record in the session

Determine whether the user is logged

Print (request.user.is_authenticated)   # determine if a user is logged is your user returns False

Once logged in users to access the user object

Print (request.user)   # If no auth.login then got an anonymous user

Check whether the user is logged

from django.contrib.auth.decorators Import   login_required 

@login_required (LOGIN_URL = ' / XXX / ' )   # local configuration 

DEF index (Request) 

    Pass 

# global configuration settings file 

LOGIN_URL = ' / XXX / '

Verify the password is correct

request.user.check_password(old_password)

change Password

request.user.set_password (new_password) 

request.user.save ()   # change your password when we must save or not save the entry into force

sign out

auth.logout(request)  # request.session.flush()

registered user

User.objects.create (username = username, password = password)   # create a user name and then use the time do not create a 

User.objects.create_user (username = username, password = password)   # Create a regular user 

User.objects.create_superuser (username = username, password = password, Email = ' [email protected] ' )   # create a super user Email (required)

Custom auth_user table

The first: do not consider using one to one relationship

The second: use classes inherit

Be sure to tell django in the configuration file

 Tell django orm no longer use the auth default table but use your custom table

In the configuration file setting.py

= AUTH_USER_MODEL ' app01.Userinfo '   # 'application name. Class name'

Using class inheritance

 
 
from django.contrib.auth.models import AbstractUser
class Userinfo (AbstractUser): 

# Do not follow the original fields in the table can only repeat Innovation 

Phone = models.BigIntegerField () 

Avatar = models.CharField (max_length = 32)

Finally, perform a database migration command

All auth module functions are all based on the table you have created, rather than using auth_user

 

Guess you like

Origin www.cnblogs.com/xiongying4/p/11588108.html