午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

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

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

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

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 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)建成功后見數(shù)據(jù)庫auth_user表

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

創(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)改變

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

認(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 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('沒有此用戶') return HttpResponse(user)

john

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

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

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

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 日本男女啪啪 | 欧美三区视频 | 黄色一级视频播放 | 日韩中文字幕亚洲 | 中文在线观看视频 | 免费看91视频 | 免费中文字幕 | 国产精品成人在线观看 | 丰满岳乱妇一区二区 | 欧美片网站免费 | 91av免费在线观看 | 四虎久久久 | 一级黄色录像大片 | 丁香六月婷婷激情 | 亚洲精品中文字幕在线观看 | 成年人国产视频 | 精品国产乱码久久久久久1区2区 | 成人毛片在线 | 黄p在线观看 | 免费av在 | 国产经典三级在线 | 在线看h| 黄色一级片在线免费观看 | 玖玖精品在线视频 | 国产欧美日韩中文字幕 | 成人av综合网| 91麻豆精品在线观看 | 久久天堂精品 | 国内成人免费视频 | 日韩av中文| 91麻豆成人 | 中文字幕99 | 97在线观视频免费观看 | 国产精品久久影院 | 一区二区三区不卡在线观看 | 成人午夜视频精品一区 | 蜜桃精品噜噜噜成人av | 欧美特级毛片 | 香蕉视频免费在线播放 | 国产视频在线观看免费 | 精品天堂|