亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

瀏覽:15日期: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’, ’[email protected]’, ’johnpassword’)

from django.contrib.auth.models import Userdef create_user(request): #auth_user # user = User.objects.create_user(’john’, ’[email protected]’, ’johnpassword’) #superuser python manage.py createsuperuser --username=joe [email protected] 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 protected]

或者

$ 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
相關文章:
主站蜘蛛池模板: 男人懂的网站 | 国产精品白嫩在线观看 | 尤物视频在线观看免费视频 | 国产婷婷综合在线精品尤物 | 大片免费看费看大片 | 欧美jizz8性欧美18 | 91视频最新地址 | 涩久久| 91精品国产自产91精品 | 俄罗斯小younv另类 | 一区二区三区四区在线视频 | 成年在线观看视频免费看 | 九九精品视频在线 | 亚洲大尺度在线 | 污网址| 国产亚洲欧美在线 | 久草视频在线首页 | 9久热久re爱免费精品视频 | 麻豆精品视频 在线视频 | 黄色a级片在线观看 | 国产亚洲精品色一区 | 亚洲人成综合网站在线 | 色aaa| 国产偷国产偷亚洲高清在线 | 久久99精品久久久久久 | 美国免费一级片 | 鲁大师手机在线观看视频 | 成人卡通精品卡通动漫第一页 | 1717she精品国产真正免费 | 男女刺激大片免费观看网站 | 伊人激情在线 | 高清欧美日韩一区二区三区在线观看 | 久久久夜色精品国产噜噜 | 日本国产在线视频 | 国产精品主播在线观看 | 日韩精品一区二区三区不卡 | 91精品国产露脸在线 | 亚洲精品日韩精品一区 | 日本黄色大片在线观看 | 亚洲精品国产第一综合99久久 | 麻豆网站在线观看 |