程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django17: built in user system

編輯:Python

1、 Definition

        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 .

2、 Basic fields

         Model class location from django.contrib.contrib.auth.models import User

  • username: user name
  • password: password
  • email: mailbox
  • first_name: name
  • last_name: surname
  • is_superuser: Whether it is an administrator account (/admin)
  • is_staff: Can I visit admin Management interface
  • is_active: Is it an active user , Default True. Generally, users are not deleted , Instead, the user's is_active Set to False
  • last_login: Last login time
  • date_joined: User creation time

3、 Create user

(1) Create Putian user create_user

from django.contrib.auth.models import user
user = User.objcets.create_user(username=’ user name ’, password=’ password ’, email=’ mailbox ’, …)

(2) Create a superuser create_superuser

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 :

4、 Delete user

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 ”)

5、 password

(1) Check the password

#  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)

(2) Change 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 ’)

6、 Sign in

(1) Login status remains

from django.contrib.auth.models import login
def login_view(request):
user = authenticate(username=username, password=password)
login(request, user)

(2) Login status verification

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
…

(3) Login status cancelled

from django.contrib.auth.models import logout
def login_view(request):
logout(request)

7、 Built in user table —— Extension field

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 :

  •          Add new apps
  •          Define model classes , Inherit AbstractUser
  •         settings.ps Named in the AUTH _USER_MODEL = ‘ Application name ; Class name ’’

Be careful : This operation should be done for the first time migrate Go ahead


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved