Django認(rèn)證系統(tǒng)user對象實(shí)現(xiàn)過程解析
User對象
User對象是認(rèn)證系統(tǒng)的核心。它們通常表示與你的站點(diǎn)進(jìn)行交互的用戶,并用于啟用限制訪問、注冊用戶信息和關(guān)聯(lián)內(nèi)容給創(chuàng)建者等。在Django的認(rèn)證框架中只存在一種類型的用戶,因此諸如'superusers'或管理員'staff'用戶只是具有特殊屬性集的user對象,而不是不同類型的user對象。
創(chuàng)建users
創(chuàng)建users最直接的方法是使用create_user()輔助函數(shù):
>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
from django.contrib.auth.models import User
def create_user(request):
#auth_user
# user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
#superuser python manage.py createsuperuser --username=joe --email=joe@example.com
u = User.objects.get(username='john')
u.set_password('new password')
u.save()
return HttpResponse("success-----%s"%u)
創(chuàng)建成功后見數(shù)據(jù)庫auth_user表

創(chuàng)建superusers
使用createsuperuser命令創(chuàng)建superusers:
$ python manage.py createsuperuser --username=joe --email=joe@example.com
或者
$ python manage.py createsuperuser
接下來依次輸入用戶密碼即可
成功后見auth_user表
修改密碼
>>> from django.contrib.auth.models import User
>>> u = User.objects.get(username='john')
>>> u.set_password('new password')
>>> u.save()
成功后見auth_user表,密碼已經(jīng)改變

認(rèn)證Users
authenticate(**credentials)[source]
認(rèn)證一個(gè)給定用戶名和密碼,請使用authenticate()。它以關(guān)鍵字參數(shù)形式接收憑證,對于默認(rèn)的配置它是username和password,如果密碼對于給定的用戶名有效它將返回一個(gè)User對象。如果密碼無效,authenticate()返回None。例子:
from django.contrib.auth import authenticate
user = authenticate(username='john', password='secret')
if user is not None:
# the password verified for the user
if user.is_active:
print()
else:
print()
else:
# the authentication system was unable to verify the username and password
print()
def auth(request):
user = authenticate(username='john', password='new password')#john
# user = authenticate(username='john', password='johnpassword')#None
print(user)
if user is not None:
# the password verified for the user
if user.is_active:
print("驗(yàn)證成功,已激活")
else:
print("驗(yàn)證成功,未激活")
else:
# the authentication system was unable to verify the username and password
print("沒有此用戶")
return HttpResponse(user)
john
驗(yàn)證成功,已激活

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Django生成數(shù)據(jù)庫及添加用戶報(bào)錯(cuò)解決方案
這篇文章主要介紹了Django生成數(shù)據(jù)庫及添加用戶報(bào)錯(cuò)解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Python 使用元類type創(chuàng)建類對象常見應(yīng)用詳解
這篇文章主要介紹了Python 使用元類type創(chuàng)建類對象,結(jié)合實(shí)例形式詳細(xì)分析了Python元類的概念、功能及元類type創(chuàng)建類對象的常見應(yīng)用技巧,需要的朋友可以參考下2019-10-10
Python Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法匯總
這篇文章主要介紹了Python中的Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法,創(chuàng)建Dataframe主要是使用pandas中的DataFrame函數(shù),其核心就是第一個(gè)參數(shù):data,傳入原始數(shù)據(jù),因此我們可以據(jù)此給出六種創(chuàng)建Dataframe的方法,需要的朋友可以參考下2023-05-05
centos 自動運(yùn)行python腳本和配置 Python 定時(shí)任務(wù)
這篇文章主要介紹了centos 自動運(yùn)行python腳本和配置 Python 定時(shí)任務(wù),文章內(nèi)容介紹詳細(xì),需要的小伙伴可以參考一下,希望對你有所幫助2022-03-03
Python計(jì)算機(jī)視覺SIFT尺度不變的圖像特征變換
這篇文章主要為大家介紹了Python計(jì)算機(jī)視覺SIFT尺度不變的圖像特征變換,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
pytorch保存和加載模型的方法及如何load部分參數(shù)
本文總結(jié)了pytorch中保存和加載模型的方法,以及在保存的模型文件與新定義的模型的參數(shù)不一一對應(yīng)時(shí),我們該如何加載模型參數(shù),對pytorch保存和加載模型相關(guān)知識感興趣的朋友一起看看吧2024-03-03

