Django使用者認證系統(一)User對象

來源:互聯網
上載者:User

User對象

User對象是認證系統的核心。使用者物件通常用來代表網站的使用者,並支援例如存取控制、註冊使用者、關聯建立者和內容等。在Django認證架構中只有一個使用者類,例如超級使用者('superusers’)或('staff')使用者只不過是相同使用者物件設定了不同屬性而已。

預設欄位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
使用者權限。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設為False。

is_superuser
Boolean。預設False。當設為True時,使用者獲得全部許可權。

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
使用者建立的時間

方法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)
設定密碼。

check_password(raw_password)
驗證密碼。

get_group_permissions(obj=None)
返回使用者組許可權的集合。

get_all_permissions(obj=None)
返回使用者所有的許可權集合。

has_perm(perm, obj=None)
使用者是否具有某個許可權。perm的格式是 "<app label>.<permission codename>".

has_perms(perm_list, obj=None)
使用者是否具有許可權列表中的每個許可權。

建立使用者

由於User對象的密碼不是明文儲存的,所以建立User對象時與通常的Model create不同,需用內建的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介面中添加使用者。

建立superusers

$ python manage.py createsuperuser --username=joe --email=joe@example.com

修改密碼

使用內建的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.")

以上就是Django使用者認證系統(一)User對象的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.