Django用戶認(rèn)證系統(tǒng) User對象解析
User對象
User對象是認(rèn)證系統(tǒng)的核心。用戶對象通常用來代表網(wǎng)站的用戶,并支持例如訪問控制、注冊用戶、關(guān)聯(lián)創(chuàng)建者和內(nèi)容等。在Django認(rèn)證框架中只有一個用戶類,例如超級用戶('superusers')或('staff')用戶只不過是相同用戶對象設(shè)置了不同屬性而已。
缺省字段Fields
username
用戶名,必需字段。30個字符或更少,可以包含 _, @, +, . 和 - 字符。
first_name
可選。 30 characters or fewer.
last_name
可選。 30 characters or fewer.
email
郵箱,可選。 Email address.
password
密碼,必需。Django不是以明文存儲密碼的,而是存儲哈希值。
groups
用戶組。Many-to-many relationship to Group
user_permissions
用戶權(quán)限。Many-to-many relationship to Permission
groups = models.ManyToManyField(Group, verbose_name=_('groups'),
blank=True, help_text=_('The groups this user belongs to. A user will '
'get all permissions granted to each of '
'their groups.'),
related_name="user_set", related_query_name="user")
user_permissions = models.ManyToManyField(Permission,
verbose_name=_('user permissions'), blank=True,
help_text=_('Specific permissions for this user.'),
related_name="user_set", related_query_name="user")
is_staff
Boolean。決定用戶是否可以訪問admin管理界面。默認(rèn)False。
is_active
Boolean。 用戶是否活躍,默認(rèn)True。一般不刪除用戶,而是將用戶的is_active設(shè)為False。
is_superuser
Boolean。默認(rèn)False。當(dāng)設(shè)為True時,用戶獲得全部權(quán)限。
def has_perm(self, perm, obj=None):
"""
Returns True if the user has the specified permission. This method
queries all available auth backends, but returns immediately if any
backend returns True. Thus, a user who has permission from a single
auth backend is assumed to have permission in general. If an object is
provided, permissions for this specific object are checked.
"""
# Active superusers have all permissions.
if self.is_active and self.is_superuser:
return True
# Otherwise we need to check the backends.
return _user_has_perm(self, perm, obj)
last_login
上一次的登錄時間,為datetime對象,默認(rèn)為當(dāng)時的時間。
user.last_login = timezone.now()
date_joined
用戶創(chuàng)建的時間
方法Methods
is_anonymous()
是否是匿名用戶。
is_authenticated()
用戶是否通過驗證,登陸。
get_full_name()
返回first_name plus the last_name, with a space in between.
get_short_name()
返回first_name.
set_password(raw_password)
設(shè)置密碼。
check_password(raw_password)
驗證密碼。
get_group_permissions(obj=None)
返回用戶組權(quán)限的集合。
get_all_permissions(obj=None)
返回用戶所有的權(quán)限集合。
has_perm(perm, obj=None)
用戶是否具有某個權(quán)限。perm的格式是 "<app label>.<permission codename>".
has_perms(perm_list, obj=None)
用戶是否具有權(quán)限列表中的每個權(quán)限。
創(chuàng)建用戶
由于User對象的密碼不是明文存儲的,所以創(chuàng)建User對象時與通常的Model create不同,需用內(nèi)置的create_user()方法。
>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
# At this point, user is a User object that has already been saved
# to the database. You can continue to change its attributes
# if you want to change other fields.
>>> user.last_name = 'Lennon'
>>> user.save()
當(dāng)然也可以在admin界面中添加用戶。
創(chuàng)建superusers
$ python manage.py createsuperuser --username=joe --email=joe@example.com
修改密碼
使用內(nèi)置的set_password()方法。
>>> from django.contrib.auth.models import User
>>> u = User.objects.get(username='john')
>>> u.set_password('new password')
>>> u.save()
驗證用戶
authenticate()
驗證給出的username和password是否是一個有效用戶。如果有效,則返回一個User對象,無效則返回None。
from django.contrib.auth import authenticate
user = authenticate(username='john', password='secret')
if user is not None:
# the password verified for the user
if user.is_active:
print("User is valid, active and authenticated")
else:
print("The password is valid, but the account has been disabled!")
else:
# the authentication system was unable to verify the username and password
print("The username and password were incorrect.")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- django基于存儲在前端的token用戶認(rèn)證解析
- Django用戶認(rèn)證系統(tǒng) 組與權(quán)限解析
- Django用戶認(rèn)證系統(tǒng) Web請求中的認(rèn)證解析
- django rest framework 實現(xiàn)用戶登錄認(rèn)證詳解
- Django 權(quán)限認(rèn)證(根據(jù)不同的用戶,設(shè)置不同的顯示和訪問權(quán)限)
- 利用Django內(nèi)置的認(rèn)證視圖實現(xiàn)用戶密碼重置功能詳解
- 深入理解Django中內(nèi)置的用戶認(rèn)證
- Django自帶用戶認(rèn)證系統(tǒng)使用方法解析
相關(guān)文章
Django使用詳解:ORM 的反向查找(related_name)
今天小編就為大家分享一篇Django使用詳解:ORM 的反向查找(related_name),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
python使用for...else跳出雙層嵌套循環(huán)的方法實例
這篇文章主要給大家介紹了關(guān)于python使用for...else跳出雙層嵌套循環(huán)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Pycharm debug調(diào)試時帶參數(shù)過程解析
這篇文章主要介紹了Pycharm debug調(diào)試時帶參數(shù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
使用Python+OpenCV進(jìn)行卡類型及16位卡號數(shù)字的OCR功能
本文將使用Python+OpenCV實現(xiàn)模板匹配算法,以自動識別卡的類型和以及16位卡號數(shù)字,通過實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-08-08
分位數(shù)回歸模型quantile regeression應(yīng)用詳解及示例教程
這篇文章主要為大家介紹了介紹了分位數(shù)回歸quantile regeression的概念詳解及代碼示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11

