国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Django認(rèn)證系統(tǒng)user對(duì)象實(shí)現(xiàn)過(guò)程解析

瀏覽:24日期:2024-10-23 14:55:25

User對(duì)象

User對(duì)象是認(rèn)證系統(tǒng)的核心。它們通常表示與你的站點(diǎn)進(jìn)行交互的用戶(hù),并用于啟用限制訪問(wèn)、注冊(cè)用戶(hù)信息和關(guān)聯(lián)內(nèi)容給創(chuàng)建者等。在Django的認(rèn)證框架中只存在一種類(lèi)型的用戶(hù),因此諸如’superusers’或管理員’staff’用戶(hù)只是具有特殊屬性集的user對(duì)象,而不是不同類(lèi)型的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 Userdef 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)建成功后見(jiàn)數(shù)據(jù)庫(kù)auth_user表

Django認(rèn)證系統(tǒng)user對(duì)象實(shí)現(xiàn)過(guò)程解析

創(chuàng)建superusers

使用createsuperuser命令創(chuàng)建superusers:

$ python manage.py createsuperuser --username=joe --email=joe@example.com

或者

$ python manage.py createsuperuser

接下來(lái)依次輸入用戶(hù)密碼即可成功后見(jiàn)auth_user表

修改密碼

>>> from django.contrib.auth.models import User>>> u = User.objects.get(username=’john’)>>> u.set_password(’new password’)>>> u.save()

成功后見(jiàn)auth_user表,密碼已經(jīng)改變

Django認(rèn)證系統(tǒng)user對(duì)象實(shí)現(xiàn)過(guò)程解析

認(rèn)證Users

authenticate(**credentials)[source]

認(rèn)證一個(gè)給定用戶(hù)名和密碼,請(qǐng)使用authenticate()。它以關(guān)鍵字參數(shù)形式接收憑證,對(duì)于默認(rèn)的配置它是username和password,如果密碼對(duì)于給定的用戶(hù)名有效它將返回一個(gè)User對(duì)象。如果密碼無(wú)效,authenticate()返回None。例子:

from django.contrib.auth import authenticateuser = 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('沒(méi)有此用戶(hù)') return HttpResponse(user)

john

驗(yàn)證成功,已激活

Django認(rèn)證系統(tǒng)user對(duì)象實(shí)現(xiàn)過(guò)程解析

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 舞钢市| 乌兰县| 山东| 茶陵县| 鹤山市| 伊吾县| 北安市| 凤台县| 齐齐哈尔市| 柘荣县| 扎鲁特旗| 潍坊市| 永清县| 巴楚县| 永嘉县| 阆中市| 深州市| 杭州市| 马山县| 湖南省| 凤庆县| 蕉岭县| 赫章县| 汨罗市| 德阳市| 贵港市| 江北区| 随州市| 耿马| 剑阁县| 积石山| 金堂县| 吴江市| 凯里市| 东乌珠穆沁旗| 金湖县| 北票市| 准格尔旗| 安阳县| 太仆寺旗| 勃利县|