Django用戶認證系統(tǒng) User對象解析
User對象
User對象是認證系統(tǒng)的核心。用戶對象通常用來代表網(wǎng)站的用戶,并支持例如訪問控制、注冊用戶、關(guān)聯(lián)創(chuàng)建者和內(nèi)容等。在Django認證框架中只有一個用戶類,例如超級用戶('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管理界面。默認False。
is_active
Boolean。 用戶是否活躍,默認True。一般不刪除用戶,而是將用戶的is_active設(shè)為False。
is_superuser
Boolean。默認False。當設(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對象,默認為當時的時間。
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()
當然也可以在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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python如何統(tǒng)計函數(shù)調(diào)用的耗時
這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)統(tǒng)計函數(shù)調(diào)用的耗時,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04pytorch nn.Conv2d()中的padding以及輸出大小方式
今天小編就為大家分享一篇pytorch nn.Conv2d()中的padding以及輸出大小方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python爬蟲爬取新浪微博內(nèi)容示例【基于代理IP】
這篇文章主要介紹了Python爬蟲爬取新浪微博內(nèi)容,結(jié)合實例形式分析了Python基于代理IP實現(xiàn)的微博爬取與抓包分析相關(guān)操作技巧,需要的朋友可以參考下2018-08-08自然語言處理之文本熱詞提取(含有《源碼》和《數(shù)據(jù)》)
這篇文章主要介紹了自然語言處理之文本熱詞提取,主要就是通過jieba的posseg模塊將一段文字分段并賦予不同字段不同意思,然后通過頻率計算出熱頻詞,需要的朋友可以參考下2022-05-05python opencv實現(xiàn)信用卡的數(shù)字識別
這篇文章主要介紹了python opencv實現(xiàn)信用卡的數(shù)字識別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01詳解Django中的ifequal和ifnotequal標簽使用
這篇文章主要介紹了詳解Django中的ifequal和ifnotequal標簽使用,Django是重多高人氣Python框架中最為著名的一個,需要的朋友可以參考下2015-07-07python實現(xiàn)各進制轉(zhuǎn)換的總結(jié)大全
這篇文章主要給大家總結(jié)了python實現(xiàn)各進制轉(zhuǎn)換的相關(guān)資料,其中包括字符串與十六進制轉(zhuǎn)換、內(nèi)置函數(shù)hex()與進制互轉(zhuǎn)等相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06python實現(xiàn)xml轉(zhuǎn)json文件的示例代碼
這篇文章主要介紹了python實現(xiàn)xml轉(zhuǎn)json文件的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12