Django用戶(hù)認(rèn)證系統(tǒng) User對(duì)象解析
User對(duì)象
User對(duì)象是認(rèn)證系統(tǒng)的核心。用戶(hù)對(duì)象通常用來(lái)代表網(wǎng)站的用戶(hù),并支持例如訪(fǎng)問(wèn)控制、注冊(cè)用戶(hù)、關(guān)聯(lián)創(chuàng)建者和內(nèi)容等。在Django認(rèn)證框架中只有一個(gè)用戶(hù)類(lèi),例如超級(jí)用戶(hù)('superusers')或('staff')用戶(hù)只不過(guò)是相同用戶(hù)對(duì)象設(shè)置了不同屬性而已。
缺省字段Fields
username
用戶(hù)名,必需字段。30個(gè)字符或更少,可以包含 _, @, +, . 和 - 字符。
first_name
可選。 30 characters or fewer.
last_name
可選。 30 characters or fewer.
email
郵箱,可選。 Email address.
password
密碼,必需。Django不是以明文存儲(chǔ)密碼的,而是存儲(chǔ)哈希值。
groups
用戶(hù)組。Many-to-many relationship to Group
user_permissions
用戶(hù)權(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。決定用戶(hù)是否可以訪(fǎng)問(wèn)admin管理界面。默認(rèn)False。
is_active
Boolean。 用戶(hù)是否活躍,默認(rèn)True。一般不刪除用戶(hù),而是將用戶(hù)的is_active設(shè)為False。
is_superuser
Boolean。默認(rèn)False。當(dāng)設(shè)為T(mén)rue時(shí),用戶(hù)獲得全部權(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
上一次的登錄時(shí)間,為datetime對(duì)象,默認(rèn)為當(dāng)時(shí)的時(shí)間。
user.last_login = timezone.now()
date_joined
用戶(hù)創(chuàng)建的時(shí)間
方法Methods
is_anonymous()
是否是匿名用戶(hù)。
is_authenticated()
用戶(hù)是否通過(guò)驗(yàn)證,登陸。
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)
驗(yàn)證密碼。
get_group_permissions(obj=None)
返回用戶(hù)組權(quán)限的集合。
get_all_permissions(obj=None)
返回用戶(hù)所有的權(quán)限集合。
has_perm(perm, obj=None)
用戶(hù)是否具有某個(gè)權(quán)限。perm的格式是 "<app label>.<permission codename>".
has_perms(perm_list, obj=None)
用戶(hù)是否具有權(quán)限列表中的每個(gè)權(quán)限。
創(chuàng)建用戶(hù)
由于User對(duì)象的密碼不是明文存儲(chǔ)的,所以創(chuàng)建User對(duì)象時(shí)與通常的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界面中添加用戶(hù)。
創(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()
驗(yàn)證用戶(hù)
authenticate()
驗(yàn)證給出的username和password是否是一個(gè)有效用戶(hù)。如果有效,則返回一個(gè)User對(duì)象,無(wú)效則返回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.")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- django基于存儲(chǔ)在前端的token用戶(hù)認(rèn)證解析
- Django用戶(hù)認(rèn)證系統(tǒng) 組與權(quán)限解析
- Django用戶(hù)認(rèn)證系統(tǒng) Web請(qǐng)求中的認(rèn)證解析
- django rest framework 實(shí)現(xiàn)用戶(hù)登錄認(rèn)證詳解
- Django 權(quán)限認(rèn)證(根據(jù)不同的用戶(hù),設(shè)置不同的顯示和訪(fǎng)問(wèn)權(quán)限)
- 利用Django內(nèi)置的認(rèn)證視圖實(shí)現(xiàn)用戶(hù)密碼重置功能詳解
- 深入理解Django中內(nèi)置的用戶(hù)認(rèn)證
- Django自帶用戶(hù)認(rèn)證系統(tǒng)使用方法解析
相關(guān)文章
Python如何統(tǒng)計(jì)函數(shù)調(diào)用的耗時(shí)
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)統(tǒng)計(jì)函數(shù)調(diào)用的耗時(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04pytorch nn.Conv2d()中的padding以及輸出大小方式
今天小編就為大家分享一篇pytorch nn.Conv2d()中的padding以及輸出大小方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python爬蟲(chóng)爬取新浪微博內(nèi)容示例【基于代理IP】
這篇文章主要介紹了Python爬蟲(chóng)爬取新浪微博內(nèi)容,結(jié)合實(shí)例形式分析了Python基于代理IP實(shí)現(xiàn)的微博爬取與抓包分析相關(guān)操作技巧,需要的朋友可以參考下2018-08-08自然語(yǔ)言處理之文本熱詞提取(含有《源碼》和《數(shù)據(jù)》)
這篇文章主要介紹了自然語(yǔ)言處理之文本熱詞提取,主要就是通過(guò)jieba的posseg模塊將一段文字分段并賦予不同字段不同意思,然后通過(guò)頻率計(jì)算出熱頻詞,需要的朋友可以參考下2022-05-05python opencv實(shí)現(xiàn)信用卡的數(shù)字識(shí)別
這篇文章主要介紹了python opencv實(shí)現(xiàn)信用卡的數(shù)字識(shí)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01詳解Django中的ifequal和ifnotequal標(biāo)簽使用
這篇文章主要介紹了詳解Django中的ifequal和ifnotequal標(biāo)簽使用,Django是重多高人氣Python框架中最為著名的一個(gè),需要的朋友可以參考下2015-07-07python實(shí)現(xiàn)各進(jìn)制轉(zhuǎn)換的總結(jié)大全
這篇文章主要給大家總結(jié)了python實(shí)現(xiàn)各進(jìn)制轉(zhuǎn)換的相關(guān)資料,其中包括字符串與十六進(jìn)制轉(zhuǎn)換、內(nèi)置函數(shù)hex()與進(jìn)制互轉(zhuǎn)等相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-06-06python實(shí)現(xiàn)xml轉(zhuǎn)json文件的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)xml轉(zhuǎn)json文件的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12