Django With a user authentication system . It handles user accounts 、 Group 、 Permission and based on cookie User session for . Users can directly use Django Self contained user table .
Model class location from django.contrib.contrib.auth.models import User
from django.contrib.auth.models import user
user = User.objcets.create_user(username=’ user name ’, password=’ password ’, email=’ mailbox ’, …)
from django.contrib.auth.models import user
user = User.objcets.create_superuser(username=’ user name ’, password=’ password ’, email=’ mailbox ’, …)
After creation, you can auth_user See in the table :
example :
from django.contrib.auth.models import User
try:
user = User.objcects.get(username=’ user name ’)
user.is_active = False # The current user is invalid
user.save()
print(“ Delete failed ”)
except:
print(“ Delete failed ”)
# If the user name and password verification is successful, it returns for user object , Otherwise return to None
from django.contrib.auth import authenticate
user = authenticate(username=username, password=password)
from django.contrib.auth.models import User
try:
user = User.objects.get(username=’aaa’)
user.set_password(‘123’)
user.save()
return HttpResponse(‘ Password changed successfully ’)
except:
return HttpResponse(‘ Password change failed ’)
from django.contrib.auth.models import login
def login_view(request):
user = authenticate(username=username, password=password)
login(request, user)
from django.contrib.auth.decorators import login_required
@login_required
def index_view(request):
# This view can only be accessed when the user is logged in
# The currently logged in user can use rquest.user obtain
login_user = request.user
…
from django.contrib.auth.models import logout
def login_view(request):
logout(request)
Method 1 : By creating a new table , Follow the built-in table 1 Yes 1
Method 2 : Inherit the built-in abstraction user Model class
step :
Be careful : This operation should be done for the first time migrate Go ahead