Django's auth component

auth module

Overview:

  The development of a site, the site is sure to be designed and implemented the user's system. At this point we need to implement their own login, authentication, log off, change their password, django its built-in user authentication system auth.

1 from django.contrib import auth

auth method:

authenticate()   

  The current user authentication

1 user = authenticate(username='sb',password='123456')

login(HttpRequest, user) 

  This function takes an HttpRequest object and a User object certified.

  This function implements a user login functionality. Essentially it generates a session related data for the user at the rear end.

 1 from django.contrib.auth import authenticate, login
 2    
 3 def my_view(request):
 4   username = request.POST['username']
 5   password = request.POST['password']
 6   user_obj = authenticate(username=username, password=password)
 7   if user_obj:
 8     login(request, user_obj)
 9     # Redirect to a success page.
10     ...
11   else:
12     # Return an 'invalid login' error message.
13     ...

  Note: After just use login (request, user_obj), request.user able to get the user object is currently logged on. Otherwise request.user get is an anonymous user object (AnonymousUser Object).

logout(request) 

  This function takes a HttpRequest object, no return value.

  When the function is called, the current request will clear all session information. Even if the user is not logged in, use this function also does not complain.

1 from django.contrib.auth import logout
2    
3 def logout_view(request):
4   logout(request)
5   # Redirect to a success page. 

is_authenticated()

  It determines whether the current request is authenticated

1 def my_view(request):
2   if not request.user.is_authenticated():
3     return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))

 login_requierd()

  auth provides us with a decorative tool for quick login to add a view to checking

1 from django.contrib.auth.decorators import login_required
2       
3 @login_required
4 def my_view(request):
5   ...

  If the user is not logged, it will jump to the django default login URL '/ accounts / login /' and pass the current url to access the absolute path (after a successful landing, will be redirected to the path).

  If you want to customize the login URL, you need to be modified by LOGIN_URL in settings.py file.

create_user()

from django.contrib.auth.models import User
user = User.objects.create_user(username='用户名',password='密码',email='邮箱',...)

create_superuser()

from django.contrib.auth.models Import the User 
USER_OBJ = User.objects.create_superuser (username = ' username ' , password = ' password ' , Email = ' E-mail ' , ...)

check_password(raw_password)

  The correct way to check whether a password auth offer, you need to provide the current request the user's password.

  The password is correct return True, otherwise False.

set_password(raw_password)

  The method of modifying a password provided by the auth received new password to be set as a parameter.

  Note: After setting sure to call the save method for user objects! ! !

1 user_obj.set_password('123456')
2 user_obj.save()

Custom auth_user table

 1 from django.contrib.auth.models import AbstractUser
 2 class UserInfo(AbstractUser):
 3     """
 4     用户信息表
 5     """
 6     nid = models.AutoField(primary_key=True)
 7     phone = models.CharField(max_length=11, null=True, unique=True)
 8     
 9     def __str__(self):
10         return self.username

In the configuration settings

1  # cited Django comes with the User table, you need to set when inheritance using 
2 AUTH_USER_MODEL = " app01.UserInfo " 

  Custom Well, of course we can use the table

general user:

1 UserInfo.objects.create_user(nid=1, password=123456)

root:

1 UserInfo.objects.create_superuser(nid=1, password=123456)

note:

  Once we specify the table new authentication system used, we need to re-create the table in the database, but can not continue to use the original default auth_user the table.

 

Guess you like

Origin www.cnblogs.com/Alexephor/p/11310486.html