A: auth foundation
(1) Action: django provided to the user module developers operated
For example: Sign cancellation of certification, etc.
(2) use
from django.contrib import auth
II: Method Description
(1)authenticate()
(1) provides user authentication function
(2) If the authentication is successful it will return a user object
<p>用户名称:<input type="text" name="username"></p> <p>用户密码:<input type="text" name="password"></p> <input type="submit">
from django.contrib import auth def user(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password')
user_obj = auth.authenticate(username=username,password=password)
return render(request,'user.html')
user_obj = auth.authenticate(username=username,password=password) print(user_obj) # admin 对象 print(user_obj.username) # admin print(user_obj.password) # pbkdf2_sha256$36000$qVMoNB0niRMM$Z+32NPaCez7yHBcLokJq+KSzmkdv3vetY+tFvhGCrC8= 密码
(2)login(HttpRequest, user)
(1) accept data input front end
(2) is provided to the front end of the user session value
auth.login(request,user_obj)
PS:
(1) As long as the code can be executed at any location then acquires user object
(2) If a user object does not exist will return an anonymous object
(3)is_authenticated
If (1) Log in to view objects
(2) Returns true if the login false otherwise
request.user.is_authenticated
(4)check_password
(1) verify the password is correct
(2) a user password which will be entered automatically compares encryption and back-end database
request.user.check_password (password)
(5)set_password
(1) the original password changes
(2) After making the changes must save command
request.user.set_password(new_password)
request.user.save()
(6)auth.logout(request)
(1) The database browser data will be cleared
(2) similar request.session.flush
auth.logout(request)
(7)login_requied
from django.contrib.auth.decorators import login_required @login_required(login_url='/xxx/') # 局部配置 def index(request): pass
PS:
(1) If you do not specify if the user is not logged login_url parameter will jump to a strange page
(2) designation can jump to a specific page
# Global configuration settings file LOGIN_URL = ' / XXX / '
(8) to create a user
# User.objects.create (username = username, password = password) # create a user name and then use the time do not create the # 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)
Three: Custom Form auth_user
# The second way to use class inheritance class Userinfo (AbstractUser): # Do not repeat the only innovation in the field with the original table Phone = models.BigIntegerField () Avatar = models.CharField (max_length = 32 ) # must be in configuration file tells Django # tell django orm no longer use the auth default table but use your custom table AUTH_USER_MODEL = ' app01.Userinfo ' # 'application name. class name'
PS:
(1) perform a database migration command
(2) all auth module functions are all based on the table you created
(3) no longer use auth_user