使用 Django 的验证系统Link to this heading
本文档介绍了 Django 验证系统在默认配置下的使用方法。默认配置满足最常见的项目需求,可以处理相当多的任务,还有一个安全的密码和权限实现。对于验证需求与默认配置不同的项目,Django 支持对身份验证进行扩展和定制。
Django 验证同时提供身份验证和授权,通常称为身份验证系统,因为这些功能在某种程度上是耦合的。
User 对象Link to this heading
用户对象是认证系统的核心。它通常代表了与你的站点交互的人员,并用于允许诸如限制访问、注册用户配置文件、将内容与创建者关联等功能。Django 的认证框架中用户只有一个类,例如 “超级管理员”或“普通管理员”只是具有特殊属性集的用户对象,而不是用户对象的不同类。
默认用户的主要属性是:
请参阅完整的API文档 full API documentation 以获得完整的参考,下面的文档主要以任务为导向。
创建用户Link to this heading
创建用户最直接的方法是使用包含 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()
如果你已经安装了 Django admin 管理后台,你也可以在 admin 管理后台交互式地创建用户:ref:create users interactively <auth-admin> 。
创建超级用户Link to this heading
通过命令行 createsuperuser 创建超级管理员:
$ python manage.py createsuperuser --username=joe --email=joe@example.com
你将会被提示输入密码,完成之后,超级管理员就被创建成功了。如果你没有填写参数 --username <createsuperuser --username> ` or :option:--email <createsuperuser --email>` ,也将会被提示输入这些值。
更改密码Link to this heading
Django 不会在用户模型里保存原始(明文)密码,而只会存储哈希值(请参阅文档 如何管理密码 documentation of how passwords are managed ) 。因此,请不要试图直接操作用户的密码,这就是创建用户需要辅助函数的原因。
更改一个用户的密码,你有几个选择:
manage.py changepassword *username* 提供了在命令行修改用户密码的方法。它会提示你输入两次新密码,如果操作成功,新密码就立刻生效。如果你没有提供参数 username ,那么将会尝试修改当前系统用户的密码。
你也可以在代码里修改密码,使用 set_password():
>>> from django.contrib.auth.models import User
>>> u = User.objects.get(username='john')
>>> u.set_password('new password')
>>> u.save()
如果你已经按照了 Django admin 管理后台,你也可以在管理后台页面修改密码(请参阅 :ref:`authentication system's admin pages ` )。
Django 还提供了允许用户自行修改密码的 :ref:`views ` 和 :ref:`forms ` 。
修改密码将会注销用户的所有会话。查看详情请参阅 Session invalidation on password change 。
验证用户Link to this heading
- authenticate(request=None, \**credentials)Link to this definition
使用
authenticate()来验证用户。它使用username和password作为参数来验证,对每个身份验证后端( authentication backend ` )进行检查。如果后端验证有效,则返回一个 :class:`~django.contrib.auth.models.User 对象。如果后端引发PermissionDenied错误,将返回None。举例:from django.contrib.auth import authenticate user = authenticate(username='john', password='secret') if user is not None: # A backend authenticated the credentials else: # No backend authenticated the credentialsrequest是可选的HttpRequest,它在身份验证后端上的authenticate()方法来传递。
Web 请求的认证Link to this heading
Django 使用 sessions 和中间件将身份验证系统挂接到请求对象中。
它们在每次请求中都会提供 request.user 属性。如果当前没有用户登录,这个属性将会被设置为 AnonymousUser ,否则将会被设置为 User 实例。
你可以使用 is_authenticated 区分两者,例如:
if request.user.is_authenticated:
# Do something for authenticated users.
...
else:
# Do something for anonymous users.
...
用户如何登陆Link to this heading
如果有一个已验证的用户想附加到当前会话(session)中,将通过 login() 函数完成。
- login(request, user, backend=None)Link to this definition
要在视图中让用户登录,使用
login()。它需要HttpRequest对象和User对象。通过 Django 的 session 框架,login()会在 session 中保存用户的ID。注意,在匿名会话期间设置的任何数据都会在用户登录后保留在会话中。
这个例子展示了如何使用
authenticate()和login(): :from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid login' error message. ...
选择验证后端Link to this heading
当用户登录时,用户 ID 和用于身份验证的后端会被保存在用户会话中。允许相同的 authentication backend 在未来的请求中获取用户详情。选择要在会话中保存的验证后端如下:
使用提供了的可选
backend参数值。使用
user.backend的值。允许配对authenticate()和login():当返回用户对象时authenticate()设置user.backend属性。使用
AUTHENTICATION_BACKENDS存在的backend。否则,抛出一个异常。
在1和2中,backend 参数和 user.backend 属性应该是完整的导入路径(像 AUTHENTICATION_BACKENDS 里的路径一样),而不是真实的后端类。
用户如何登出Link to this heading
- logout(request)Link to this definition
如果已经通过
django.contrib.auth.login()登录的用户想退出登录,可以在视图中使用django.contrib.auth.logout()。需要传入HttpRequest对象,并且该函数不会返回值。例如:from django.contrib.auth import logout def logout_view(request): logout(request) # Redirect to a success page.注意,如果用户未登录,
logout()不会报错。调用
logout()后,当前请求的会话数据会被全部清除。这是为了防止其他使用同一个浏览器的用户访问前一名用户的会话数据。如果想在登出后立即向用户提供的会话中放入任何内容,请在调用django.contrib.auth.logout()之后执行此操作。
限制对登录用户的访问Link to this heading
原始方式Link to this heading
限制访问页面最简单的办法就是检查 request.user.is_authenticated 并重定向到登录页面。
from django.conf import settings
from django.shortcuts import redirect
def my_view(request):
if not request.user.is_authenticated:
return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
# ...
或者显示一个错误信息:
from django.shortcuts import render
def my_view(request):
if not request.user.is_authenticated:
return render(request, 'myapp/login_error.html')
# ...
login_required 装饰器Link to this heading
- login_required(redirect_field_name='next', login_url=None)Link to this definition
作为快捷方式,你可以使用
login_required()装饰器:from django.contrib.auth.decorators import login_required @login_required def my_view(request): ...login_required()会执行以下操作:如果用户没有登录,会重定向到
settings.LOGIN_URL,并传递绝对路径到查询字符串中。例如:/accounts/login/?next=/polls/3/。如果用户已经登录,则正常执行视图。视图里的代码可以假设用户已经登录了。
默认情况下,成功验证时用户跳转的路径保存在名为
"next"的查询字符串参数中。如果你希望这个参数使用不同名称,请在login_required()中传递可选参数redirect_field_name:from django.contrib.auth.decorators import login_required @login_required(redirect_field_name='my_redirect_field') def my_view(request): ...注意,如果你提供了
redirect_field_name值,则很可能也需要自定义登录模板,因为存储重定向路径的模板上下文变量使用的是redirect_field_name值,而不是"next"(默认情况下)。login_required()也有可选参数login_url。例如:from django.contrib.auth.decorators import login_required @login_required(login_url='/accounts/login/') def my_view(request): ...注意,如果你没有指定参数
login_url,你需要确认settings.LOGIN_URL和登录视图是正确关联的。例如,使用默认方式,在 URL 配置文件里添加下面这行:from django.contrib.auth import views as auth_views path('accounts/login/', auth_views.LoginView.as_view()),settings.LOGIN_URL也接受视图方法名和 named URL patterns 。这样你可以在 URLconf 里自由地重新映射你的登录视图,而不需更新配置文件。
LoginRequired MixinLink to this heading
使用基于类的视图时,可以使用 LoginRequiredMixin 实现和 login_required 相同的行为。这个 Mixin 应该在继承列表中最左侧的位置。
- class LoginRequiredMixinLink to this definition
如果一个视图使用 Mixin ,那么未经验证用户的所有请求都会被重定向到登录页面或者显示 HTTP 403 Forbidden 错误,这取决于
raise_exception参数。你可以设置
AccessMixin的任何参数来自定义未验证用户的处理:from django.contrib.auth.mixins import LoginRequiredMixin class MyView(LoginRequiredMixin, View): login_url = '/login/' redirect_field_name = 'redirect_to'
限制对通过测试的登录用户的访问Link to this heading
根据某些权限或者其他测试来限制访问,你基本上可以执行和上一节所述同样的操作。
最简单的方法是在视图里直接对 request.user 进行测试。举例,这个视图检查用户是否拥有特定域名的邮箱,否则会重定向到登录页:
from django.shortcuts import redirect
def my_view(request):
if not request.user.email.endswith('@example.com'):
return redirect('/login/?next=%s' % request.path)
# ...
- user_passes_test(test_func, login_url=None, redirect_field_name='next')Link to this definition
作为快捷方式,你可以方便的调用
user_passes_test装饰器,当调用返回False时会执行重定向。from django.contrib.auth.decorators import user_passes_test def email_check(user): return user.email.endswith('@example.com') @user_passes_test(email_check) def my_view(request): ...user_passes_test()接受一个必要的参数:一个带有:class:~django.contrib.auth.models.User 对象的调用,如果允许用户访问这个页面,则返回True。注意,user_passes_test()不会自动检查用户是否匿名。user_passes_test()可以传递两个可选参数:login_url允许你指定用户没有通过测试时跳转的地址。它可能是一个登录页面,如果你没指定,默认是
settings.LOGIN_URL。redirect_field_name与
login_required()相同。如果你想把没通过检查的用户重定向到没有 "next page" 的非登录页面时,把它设置为None,这样它会在 URL 中移除。
例如:
@user_passes_test(email_check, login_url='/login/') def my_view(request): ...
- class UserPassesTestMixinLink to this definition
使用基于类的视图时,可以使用
UserPassesTestMixin执行此操作。- test_func()Link to this definition
你必须覆盖类方法
test_func()以提供执行的测试。此外,还可以设置AccessMixin的任何参数来自定义处理未授权用户:from django.contrib.auth.mixins import UserPassesTestMixin class MyView(UserPassesTestMixin, View): def test_func(self): return self.request.user.email.endswith('@example.com')
- get_test_func()Link to this definition
你也可以覆盖
get_test_func()方法,以使 mixin 对其检查使用不同名称的函数(而不是test_func())。
permission_required 装饰器Link to this heading
- permission_required(perm, login_url=None, raise_exception=False)Link to this definition
It's a relatively common task to check whether a user has a particular permission. For that reason, Django provides a shortcut for that case: the
permission_required()decorator.:from django.contrib.auth.decorators import permission_required @permission_required('polls.can_vote') def my_view(request): ...Just like the
has_perm()method, permission names take the form"<app label>.<permission codename>"(i.e.polls.can_votefor a permission on a model in thepollsapplication).The decorator may also take an iterable of permissions, in which case the user must have all of the permissions in order to access the view.
Note that
permission_required()also takes an optionallogin_urlparameter:from django.contrib.auth.decorators import permission_required @permission_required('polls.can_vote', login_url='/loginpage/') def my_view(request): ...As in the
login_required()decorator,login_urldefaults tosettings.LOGIN_URL.If the
raise_exceptionparameter is given, the decorator will raisePermissionDenied, prompting the 403 (HTTP Forbidden) view instead of redirecting to the login page.If you want to use
raise_exceptionbut also give your users a chance to login first, you can add thelogin_required()decorator:from django.contrib.auth.decorators import login_required, permission_required @login_required @permission_required('polls.can_vote', raise_exception=True) def my_view(request): ...This also avoids a redirect loop when
LoginView'sredirect_authenticated_user=Trueand the logged-in user doesn't have all of the required permissions.
The PermissionRequiredMixin mixinLink to this heading
To apply permission checks to class-based views, you can use the PermissionRequiredMixin:
- class PermissionRequiredMixinLink to this definition
This mixin, just like the
permission_requireddecorator, checks whether the user accessing a view has all given permissions. You should specify the permission (or an iterable of permissions) using thepermission_requiredparameter:from django.contrib.auth.mixins import PermissionRequiredMixin class MyView(PermissionRequiredMixin, View): permission_required = 'polls.can_vote' # Or multiple of permissions: permission_required = ('polls.can_open', 'polls.can_edit')You can set any of the parameters of
AccessMixinto customize the handling of unauthorized users.你可能同样需要重写这些方法:
- get_permission_required()Link to this definition
Returns an iterable of permission names used by the mixin. Defaults to the
permission_requiredattribute, converted to a tuple if necessary.
- has_permission()Link to this definition
Returns a boolean denoting whether the current user has permission to execute the decorated view. By default, this returns the result of calling
has_perms()with the list of permissions returned byget_permission_required().
Authentication ViewsLink to this heading
Django provides several views that you can use for handling login, logout, and password management. These make use of the stock auth forms but you can pass in your own forms as well.
Django provides no default template for the authentication views. You should create your own templates for the views you want to use. The template context is documented in each view, see All authentication views.
Using the viewsLink to this heading
There are different methods to implement these views in your project. The
easiest way is to include the provided URLconf in django.contrib.auth.urls
in your own URLconf, for example:
urlpatterns = [
path('accounts/', include('django.contrib.auth.urls')),
]
This will include the following URL patterns:
accounts/login/ [name='login']
accounts/logout/ [name='logout']
accounts/password_change/ [name='password_change']
accounts/password_change/done/ [name='password_change_done']
accounts/password_reset/ [name='password_reset']
accounts/password_reset/done/ [name='password_reset_done']
accounts/reset/<uidb64>/<token>/ [name='password_reset_confirm']
accounts/reset/done/ [name='password_reset_complete']
The views provide a URL name for easier reference. See the URL documentation for details on using named URL patterns.
If you want more control over your URLs, you can reference a specific view in your URLconf:
from django.contrib.auth import views as auth_views
urlpatterns = [
path('change-password/', auth_views.PasswordChangeView.as_view()),
]
The views have optional arguments you can use to alter the behavior of the
view. For example, if you want to change the template name a view uses, you can
provide the template_name argument. A way to do this is to provide keyword
arguments in the URLconf, these will be passed on to the view. For example:
urlpatterns = [
path(
'change-password/',
auth_views.PasswordChangeView.as_view(template_name='change-password.html'),
),
]
All views are class-based, which allows you to easily customize them by subclassing.
All authentication viewsLink to this heading
This is a list with all the views django.contrib.auth provides. For
implementation details see Using the views.
- class LoginViewLink to this definition
URL name:
loginSee the URL documentation for details on using named URL patterns.
Attributes:
template_name: The name of a template to display for the view used to log the user in. Defaults toregistration/login.html.redirect_field_name: The name of aGETfield containing the URL to redirect to after login. Defaults tonext.authentication_form: A callable (typically just a form class) to use for authentication. Defaults toAuthenticationForm.extra_context: A dictionary of context data that will be added to the default context data passed to the template.redirect_authenticated_user: A boolean that controls whether or not authenticated users accessing the login page will be redirected as if they had just successfully logged in. Defaults toFalse.success_url_allowed_hosts: Asetof hosts, in addition torequest.get_host(), that are safe for redirecting after login. Defaults to an emptyset.
Here's what
LoginViewdoes:If called via
GET, it displays a login form that POSTs to the same URL. More on this in a bit.If called via
POSTwith user submitted credentials, it tries to log the user in. If login is successful, the view redirects to the URL specified innext. Ifnextisn't provided, it redirects tosettings.LOGIN_REDIRECT_URL(which defaults to/accounts/profile/). If login isn't successful, it redisplays the login form.
It's your responsibility to provide the html for the login template , called
registration/login.htmlby default. This template gets passed four template context variables:form: AFormobject representing theAuthenticationForm.next: The URL to redirect to after successful login. This may contain a query string, too.site: The currentSite, according to theSITE_IDsetting. If you don't have the site framework installed, this will be set to an instance ofRequestSite, which derives the site name and domain from the currentHttpRequest.site_name: An alias forsite.name. If you don't have the site framework installed, this will be set to the value ofrequest.META['SERVER_NAME']. For more on sites, see The "sites" framework.
If you'd prefer not to call the template
registration/login.html, you can pass thetemplate_nameparameter via the extra arguments to theas_viewmethod in your URLconf. For example, this URLconf line would usemyapp/login.htmlinstead:path('accounts/login/', auth_views.LoginView.as_view(template_name='myapp/login.html')),You can also specify the name of the
GETfield which contains the URL to redirect to after login usingredirect_field_name. By default, the field is callednext.Here's a sample
registration/login.htmltemplate you can use as a starting point. It assumes you have abase.htmltemplate that defines acontentblock:{% extends "base.html" %} {% block content %} {% if form.errors %} <p>Your username and password didn't match. Please try again.</p> {% endif %} {% if next %} {% if user.is_authenticated %} <p>Your account doesn't have access to this page. To proceed, please login with an account that has access.</p> {% else %} <p>Please login to see this page.</p> {% endif %} {% endif %} <form method="post" action="{% url 'login' %}"> {% csrf_token %} <table> <tr> <td>{{ form.username.label_tag }}</td> <td>{{ form.username }}</td> </tr> <tr> <td>{{ form.password.label_tag }}</td> <td>{{ form.password }}</td> </tr> </table> <input type="submit" value="login"> <input type="hidden" name="next" value="{{ next }}"> </form> {# Assumes you setup the password_reset view in your URLconf #} <p><a href="{% url 'password_reset' %}">Lost password?</a></p> {% endblock %}If you have customized authentication (see Customizing Authentication) you can use a custom authentication form by setting the
authentication_formattribute. This form must accept arequestkeyword argument in its__init__()method and provide aget_user()method which returns the authenticated user object (this method is only ever called after successful form validation).
- class LogoutViewLink to this definition
Logs a user out.
URL name:
logoutAttributes:
next_page: The URL to redirect to after logout. Defaults tosettings.LOGOUT_REDIRECT_URL.template_name: The full name of a template to display after logging the user out. Defaults toregistration/logged_out.html.redirect_field_name: The name of aGETfield containing the URL to redirect to after log out. Defaults tonext. Overrides thenext_pageURL if the givenGETparameter is passed.extra_context: A dictionary of context data that will be added to the default context data passed to the template.success_url_allowed_hosts: Asetof hosts, in addition torequest.get_host(), that are safe for redirecting after logout. Defaults to an emptyset.
Template context:
title: The string "Logged out", localized.site: The currentSite, according to theSITE_IDsetting. If you don't have the site framework installed, this will be set to an instance ofRequestSite, which derives the site name and domain from the currentHttpRequest.site_name: An alias forsite.name. If you don't have the site framework installed, this will be set to the value ofrequest.META['SERVER_NAME']. For more on sites, see The "sites" framework.
- logout_then_login(request, login_url=None)Link to this definition
Logs a user out, then redirects to the login page.
URL name: No default URL provided
Optional arguments:
login_url: The URL of the login page to redirect to. Defaults tosettings.LOGIN_URLif not supplied.
- class PasswordChangeViewLink to this definition
URL name:
password_changeAllows a user to change their password.
Attributes:
template_name: The full name of a template to use for displaying the password change form. Defaults toregistration/password_change_form.htmlif not supplied.success_url: The URL to redirect to after a successful password change.form_class: A custom "change password" form which must accept auserkeyword argument. The form is responsible for actually changing the user's password. Defaults toPasswordChangeForm.extra_context: A dictionary of context data that will be added to the default context data passed to the template.
Template context:
form: The password change form (seeform_classabove).
- class PasswordChangeDoneViewLink to this definition
URL name:
password_change_doneThe page shown after a user has changed their password.
Attributes:
template_name: The full name of a template to use. Defaults toregistration/password_change_done.htmlif not supplied.extra_context: A dictionary of context data that will be added to the default context data passed to the template.
- class PasswordResetViewLink to this definition
URL name:
password_resetAllows a user to reset their password by generating a one-time use link that can be used to reset the password, and sending that link to the user's registered email address.
If the email address provided does not exist in the system, this view won't send an email, but the user won't receive any error message either. This prevents information leaking to potential attackers. If you want to provide an error message in this case, you can subclass
PasswordResetFormand use theform_classattribute.Users flagged with an unusable password (see
set_unusable_password()aren't allowed to request a password reset to prevent misuse when using an external authentication source like LDAP. Note that they won't receive any error message since this would expose their account's existence but no mail will be sent either.Attributes:
template_name: The full name of a template to use for displaying the password reset form. Defaults toregistration/password_reset_form.htmlif not supplied.form_class: Form that will be used to get the email of the user to reset the password for. Defaults toPasswordResetForm.email_template_name: The full name of a template to use for generating the email with the reset password link. Defaults toregistration/password_reset_email.htmlif not supplied.subject_template_name: The full name of a template to use for the subject of the email with the reset password link. Defaults toregistration/password_reset_subject.txtif not supplied.token_generator: Instance of the class to check the one time link. This will default todefault_token_generator, it's an instance ofdjango.contrib.auth.tokens.PasswordResetTokenGenerator.success_url: The URL to redirect to after a successful password reset request.from_email: A valid email address. By default Django uses theDEFAULT_FROM_EMAIL.extra_context: A dictionary of context data that will be added to the default context data passed to the template.html_email_template_name: The full name of a template to use for generating atext/htmlmultipart email with the password reset link. By default, HTML email is not sent.extra_email_context: A dictionary of context data that will be available in the email template.
Template context:
form: The form (seeform_classabove) for resetting the user's password.
Email template context:
email: An alias foruser.emailuser: The currentUser, according to theemailform field. Only active users are able to reset their passwords (User.is_active is True).site_name: An alias forsite.name. If you don't have the site framework installed, this will be set to the value ofrequest.META['SERVER_NAME']. For more on sites, see The "sites" framework.domain: An alias forsite.domain. If you don't have the site framework installed, this will be set to the value ofrequest.get_host().protocol: http or httpsuid: The user's primary key encoded in base 64.token: Token to check that the reset link is valid.
Sample
registration/password_reset_email.html(email body template):Someone asked for password reset for email {{ email }}. Follow the link below: {{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}The same template context is used for subject template. Subject must be single line plain text string.
- class PasswordResetDoneViewLink to this definition
URL name:
password_reset_doneThe page shown after a user has been emailed a link to reset their password. This view is called by default if the
PasswordResetViewdoesn't have an explicitsuccess_urlURL set.Attributes:
template_name: The full name of a template to use. Defaults toregistration/password_reset_done.htmlif not supplied.extra_context: A dictionary of context data that will be added to the default context data passed to the template.
- class PasswordResetConfirmViewLink to this definition
URL name:
password_reset_confirmPresents a form for entering a new password.
Keyword arguments from the URL:
uidb64: The user's id encoded in base 64.token: Token to check that the password is valid.
Attributes:
template_name: The full name of a template to display the confirm password view. Default value isregistration/password_reset_confirm.html.token_generator: Instance of the class to check the password. This will default todefault_token_generator, it's an instance ofdjango.contrib.auth.tokens.PasswordResetTokenGenerator.post_reset_login: A boolean indicating if the user should be automatically authenticated after a successful password reset. Defaults toFalse.post_reset_login_backend: A dotted path to the authentication backend to use when authenticating a user ifpost_reset_loginisTrue. Required only if you have multipleAUTHENTICATION_BACKENDSconfigured. Defaults toNone.form_class: Form that will be used to set the password. Defaults toSetPasswordForm.success_url: URL to redirect after the password reset done. Defaults to'password_reset_complete'.extra_context: A dictionary of context data that will be added to the default context data passed to the template.
Template context:
form: The form (seeform_classabove) for setting the new user's password.validlink: Boolean, True if the link (combination ofuidb64andtoken) is valid or unused yet.
- class PasswordResetCompleteViewLink to this definition
URL name:
password_reset_completePresents a view which informs the user that the password has been successfully changed.
Attributes:
template_name: The full name of a template to display the view. Defaults toregistration/password_reset_complete.html.extra_context: A dictionary of context data that will be added to the default context data passed to the template.
Helper functionsLink to this heading
- redirect_to_login(next, login_url=None, redirect_field_name='next')Link to this definition
Redirects to the login page, and then back to another URL after a successful login.
Required arguments:
next: The URL to redirect to after a successful login.
Optional arguments:
login_url: The URL of the login page to redirect to. Defaults tosettings.LOGIN_URLif not supplied.redirect_field_name: The name of aGETfield containing the URL to redirect to after log out. Overridesnextif the givenGETparameter is passed.
Built-in formsLink to this heading
If you don't want to use the built-in views, but want the convenience of not
having to write forms for this functionality, the authentication system
provides several built-in forms located in django.contrib.auth.forms:
- class AdminPasswordChangeFormLink to this definition
A form used in the admin interface to change a user's password.
Takes the
useras the first positional argument.
- class AuthenticationFormLink to this definition
A form for logging a user in.
Takes
requestas its first positional argument, which is stored on the form instance for use by sub-classes.- confirm_login_allowed(user)Link to this definition
By default,
AuthenticationFormrejects users whoseis_activeflag is set toFalse. You may override this behavior with a custom policy to determine which users can log in. Do this with a custom form that subclassesAuthenticationFormand overrides theconfirm_login_allowed()method. This method should raise aValidationErrorif the given user may not log in.For example, to allow all users to log in regardless of "active" status:
from django.contrib.auth.forms import AuthenticationForm class AuthenticationFormWithInactiveUsersOkay(AuthenticationForm): def confirm_login_allowed(self, user): pass(In this case, you'll also need to use an authentication backend that allows inactive users, such as
AllowAllUsersModelBackend.)Or to allow only some active users to log in:
class PickyAuthenticationForm(AuthenticationForm): def confirm_login_allowed(self, user): if not user.is_active: raise forms.ValidationError( _("This account is inactive."), code='inactive', ) if user.username.startswith('b'): raise forms.ValidationError( _("Sorry, accounts starting with 'b' aren't welcome here."), code='no_b_users', )
- class PasswordChangeFormLink to this definition
A form for allowing a user to change their password.
- class PasswordResetFormLink to this definition
A form for generating and emailing a one-time use link to reset a user's password.
- send_mail(subject_template_name, email_template_name, context, from_email, to_email, html_email_template_name=None)Link to this definition
Uses the arguments to send an
EmailMultiAlternatives. Can be overridden to customize how the email is sent to the user.- Parameters:
subject_template_name -- the template for the subject.
email_template_name -- the template for the email body.
context -- context passed to the
subject_template,email_template, andhtml_email_template(if it is notNone).from_email -- the sender's email.
to_email -- the email of the requester.
html_email_template_name -- the template for the HTML body; defaults to
None, in which case a plain text email is sent.
By default,
save()populates thecontextwith the same variables thatPasswordResetViewpasses to its email context.
- class SetPasswordFormLink to this definition
A form that lets a user change their password without entering the old password.
- class UserChangeFormLink to this definition
A form used in the admin interface to change a user's information and permissions.
- class UserCreationFormLink to this definition
A
ModelFormfor creating a new user.It has three fields:
username(from the user model),password1, andpassword2. It verifies thatpassword1andpassword2match, validates the password usingvalidate_password(), and sets the user's password usingset_password().
Authentication data in templatesLink to this heading
The currently logged-in user and their permissions are made available in the
template context when you use
RequestContext.
UsersLink to this heading
When rendering a template RequestContext, the
currently logged-in user, either a User
instance or an AnonymousUser instance, is
stored in the template variable {{ user }}:
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
<p>Welcome, new user. Please log in.</p>
{% endif %}
This template context variable is not available if a RequestContext is not
being used.
权限Link to this heading
The currently logged-in user's permissions are stored in the template variable
{{ perms }}. This is an instance of
django.contrib.auth.context_processors.PermWrapper, which is a
template-friendly proxy of permissions.
Evaluating a single-attribute lookup of {{ perms }} as a boolean is a proxy
to User.has_module_perms(). For example, to check if
the logged-in user has any permissions in the foo app:
{% if perms.foo %}
Evaluating a two-level-attribute lookup as a boolean is a proxy to
User.has_perm(). For example,
to check if the logged-in user has the permission foo.can_vote:
{% if perms.foo.can_vote %}
以下是一个在模板中检查权限的更完整的示例:
{% if perms.foo %}
<p>You have permission to do something in the foo app.</p>
{% if perms.foo.can_vote %}
<p>You can vote!</p>
{% endif %}
{% if perms.foo.can_drive %}
<p>You can drive!</p>
{% endif %}
{% else %}
<p>You don't have permission to do anything in the foo app.</p>
{% endif %}
It is possible to also look permissions up by {% if in %} statements.
For example:
{% if 'foo' in perms %}
{% if 'foo.can_vote' in perms %}
<p>In lookup works, too.</p>
{% endif %}
{% endif %}
Managing users in the adminLink to this heading
When you have both django.contrib.admin and django.contrib.auth
installed, the admin provides a convenient way to view and manage users,
groups, and permissions. Users can be created and deleted like any Django
model. Groups can be created, and permissions can be assigned to users or
groups. A log of user edits to models made within the admin is also stored and
displayed.
创建用户Link to this heading
You should see a link to "Users" in the "Auth" section of the main admin index page. The "Add user" admin page is different than standard admin pages in that it requires you to choose a username and password before allowing you to edit the rest of the user's fields.
Also note: if you want a user account to be able to create users using the Django admin site, you'll need to give them permission to add users and change users (i.e., the "Add user" and "Change user" permissions). If an account has permission to add users but not to change them, that account won't be able to add users. Why? Because if you have permission to add users, you have the power to create superusers, which can then, in turn, change other users. So Django requires add and change permissions as a slight security measure.
Be thoughtful about how you allow users to manage permissions. If you give a non-superuser the ability to edit users, this is ultimately the same as giving them superuser status because they will be able to elevate permissions of users including themselves!
更改密码Link to this heading
User passwords are not displayed in the admin (nor stored in the database), but the password storage details are displayed. Included in the display of this information is a link to a password change form that allows admins to change user passwords.