av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Django認證系統user對象實現過程解析

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

User對象

User對象是認證系統的核心。它們通常表示與你的站點進行交互的用戶,并用于啟用限制訪問、注冊用戶信息和關聯內容給創建者等。在Django的認證框架中只存在一種類型的用戶,因此諸如’superusers’或管理員’staff’用戶只是具有特殊屬性集的user對象,而不是不同類型的user對象。

創建users

創建users最直接的方法是使用create_user()輔助函數:

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

創建成功后見數據庫auth_user表

Django認證系統user對象實現過程解析

創建superusers

使用createsuperuser命令創建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表,密碼已經改變

Django認證系統user對象實現過程解析

認證Users

authenticate(**credentials)[source]

認證一個給定用戶名和密碼,請使用authenticate()。它以關鍵字參數形式接收憑證,對于默認的配置它是username和password,如果密碼對于給定的用戶名有效它將返回一個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('驗證成功,已激活') else: print('驗證成功,未激活') else: # the authentication system was unable to verify the username and password print('沒有此用戶') return HttpResponse(user)

john

驗證成功,已激活

Django認證系統user對象實現過程解析

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Django
相關文章:
主站蜘蛛池模板: 一区二区三区国产视频 | 蜜桃av一区二区三区 | 国产精品高清在线观看 | 丰满岳乱妇一区二区 | 天堂av影院 | 能看的黄色网址 | 久久精品欧美一区二区三区不卡 | 日韩精品毛片 | 黄色一级片黄色一级片 | 一二三四区在线观看 | 日本精品中文字幕 | 日韩欧美中文字幕在线观看 | 欧美久久一区 | 欧美在线一区二区 | 91午夜精品亚洲一区二区三区 | 日韩综合精品 | 老女人性生活视频 | 日本免费观看视频 | 成人av一区二区三区在线观看 | 99精品网站| 伊人av影院 | 国产亚洲视频在线观看 | 日本黄色免费看 | 日韩精品久久久久久久 | 成人免费福利视频 | 欧美bbb | 久久成人免费视频 | 亚洲精品观看 | 久久精品网 | 久久xxxx | 久久国产精品免费视频 | 日本精品视频在线 | 国产成人精品一区二区三区在线 | 日韩少妇av | 一区二区黄色 | 国产高清自拍视频 | 欧美精品一区在线 | 国产老头视频 | 国产福利在线观看 | 日韩在线视频播放 | 日本美女性生活 |