色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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

瀏覽:42日期: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
相關文章:
主站蜘蛛池模板: 美国一级毛片视频 | 国产精彩视频在线 | 孩交啪啪网址 | 欧美手机看片 | 青草青99久久99九九99九九九 | 国产亚洲精品久久麻豆 | ffee性xxⅹ另类老妇hd | 国产欧美日韩三级 | 久草免费在线观看 | 一区二区伦理 | 精品成人在线观看 | 日韩一区二区三区不卡视频 | 狠狠色丁香婷婷综合久久来 | 99这里只有精品视频 | 国产成人亚洲精品一区二区在线看 | 国产精品亚洲成在人线 | 国产精品久久久久久久久久久不卡 | 久草国产在线观看 | 91精品国产乱码久久久久久 | 日本阿v视频在线观看高清 日本波多野结衣视频 | 在线观看国产精品一区 | 99精品偷自拍 | 欧美精品做人一级爱免费 | 成人网18免费| 国产精品国产三级国产专区5o | 亚洲网站黄色 | 国产日韩亚洲欧美 | 亚洲精品人成网线在线 | 人人操美女| 免费在线亚洲 | 一区二区三区在线免费观看视频 | 成人毛片免费观看视频在线 | 2021一本久道 | 国产美女视频网站 | 国产成人精品高清免费 | 欧美一级做一a做片性视频 欧美一级做一级爱a做片性 | 久久经典免费视频 | 99久热在线精品视频播 | 中国老妇色xxxxx | 国产片毛片 | 80岁色老头69av|