Django認(rèn)證系統(tǒng)user對(duì)象實(shí)現(xiàn)過程解析
User對(duì)象
User對(duì)象是認(rèn)證系統(tǒng)的核心。它們通常表示與你的站點(diǎn)進(jìn)行交互的用戶,并用于啟用限制訪問、注冊(cè)用戶信息和關(guān)聯(lián)內(nèi)容給創(chuàng)建者等。在Django的認(rèn)證框架中只存在一種類型的用戶,因此諸如'superusers'或管理員'staff'用戶只是具有特殊屬性集的user對(duì)象,而不是不同類型的user對(duì)象。
創(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ù)庫(kù)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è)給定用戶名和密碼,請(qǐng)使用authenticate()。它以關(guān)鍵字參數(shù)形式接收憑證,對(duì)于默認(rèn)的配置它是username和password,如果密碼對(duì)于給定的用戶名有效它將返回一個(gè)User對(duì)象。如果密碼無效,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)證成功,已激活
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
anaconda中安裝的python環(huán)境中沒有pip3的問題及解決
這篇文章主要介紹了anaconda中安裝的python環(huán)境中沒有pip3的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02python selenium實(shí)現(xiàn)發(fā)送帶附件的郵件代碼實(shí)例
這篇文章主要介紹了python selenium實(shí)現(xiàn)發(fā)送帶附件的郵件代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12自然語言處理NLP TextRNN實(shí)現(xiàn)情感分類
這篇文章主要為大家介紹了自然語言處理NLP TextRNN實(shí)現(xiàn)情感分類示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Python OpenCV處理圖像之濾鏡和圖像運(yùn)算
這篇文章主要為大家詳細(xì)介紹了Python OpenCV處理圖像之濾鏡和圖像運(yùn)算,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07python 數(shù)字轉(zhuǎn)換為日期的三種實(shí)現(xiàn)方法
在Python中,我們經(jīng)常需要處理日期和時(shí)間,本文主要介紹了python 數(shù)字轉(zhuǎn)換為日期的三種實(shí)現(xiàn)方法,包含datetime模塊,strftime方法及pandas庫(kù),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02Flask創(chuàng)建并運(yùn)行數(shù)據(jù)庫(kù)遷移的實(shí)現(xiàn)過程
Flask創(chuàng)建并運(yùn)行數(shù)據(jù)庫(kù)遷移的過程是一個(gè)涉及多個(gè)步驟的操作,旨在幫助開發(fā)者在開發(fā)過程中管理數(shù)據(jù)庫(kù)模式的變化,而不需要手動(dòng)地刪除和重建數(shù)據(jù)庫(kù)表,從而避免數(shù)據(jù)丟失,以下是一個(gè)詳細(xì)的步驟說明,需要的朋友可以參考下2024-09-09