First, what is Auth module
Auth module is a subscriber identity module carrying Django, auth_user default table to store user data.
Second, the use
1. Create a superuser
python3 manage.py createsuperuser
Enter your user name, e-mail (you can not enter), password, confirm password (superuser successfully create and insert data in a table auth_user, since not all encrypted password must manually insert)
2. Verify user
from django.contrib Import auth # equivalent to the query: the User = models.User.objects.filter (name = name, pwd = pwd) .first () the User = auth.authenticate (Request, username = name, password = pwd) # If the check returns a user object, the user object is determined, whether the validation check
3. Log in
auth.login (Request, the User) # is actually written in a data session
Once the login is successful , the transfer function login (request, user), later view class, function in the request object, there is a user object, the user object is currently logged on; if not logged in , request.user = AnonymousUser is Anonymous User.
4. Log off
auth.logout (Request) # Internal: Call the request.session.flush (), delete the login status
The login authentication decorator
from django.contrib.auth.decorators Import login_required @login_required (redirect_field_name = ' AWSL ' , LOGIN_URL = ' / the Login / ' ) # can be locally configured # # redirect_field_name:? modify the key values behind, # LOGIN_URL: If not logged in, jump go to page # can global configuration (in the setting) # global configuration, if not logged in, skip this route LOGIN_URL = ' / the Login / '
7. Create a user
from django.contrib.auth.models Import the User # create the super user and ordinary users # can not create, not create a password encryption # user = User.objects.create (username = name, password = pwd) # Create a superuser user = User.objects.create_superuser (username = name, password = pwd) # create a regular user user = User.objects.create_user (username = name, password = pwd)
8. password verification
# Get the first user (user can log in, you can now check) request.user.check_password (pwd)
9. Change Password
user.set_password (pwd) user.save () # sure to call the save (), otherwise it is not saved
10. verification test
is_authenticated (), if validated, the contrary is true false
11. Other methods
is_active: banned from the site (user exists, the title)
is_staff: Are there administrative rights on the site (can not log admin)
12. Delete User
orm delete
Third, extend the default table auth_user
Method One: one table and do auth_user association
from django.contrib.auth.models Import the User class UserDetail (models.Model): Phone = models.CharField (MAX_LENGTH = 32 ) # one auth_user Correlative with table # If the model table is introduced from the outside, is not added quotes # if quoted, just looking at the current Model the User = models.OneToOneField (= to the User)
Method Two: inherited AbstractUser
from django.contrib.auth.models Import AbstractUser class the UserInfo (AbstractUser): # username, password auth_user table until the field has Phone = models.CharField (MAX_LENGTH = 32 ) Sex = models.BooleanField ()
# arranged in the setting do database migration, since there is no auth_user this table, with certified components is UserInfo table AUTH_USER_MODEL = ' app01.UserInfo '