---
title: "使用 Django 的验证系统"
version: 2.1
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/2.1/topics/auth/default/
canonical: https://djangodocs.dev/zh-hans/2.1/topics/auth/default/
---
# 使用 Django 的验证系统

本文档介绍了 Django 验证系统在默认配置下的使用方法。默认配置满足最常见的项目需求，可以处理相当多的任务，还有一个安全的密码和权限实现。对于验证需求与默认配置不同的项目，Django 支持对身份验证进行扩展和定制。

Django 验证同时提供身份验证和授权，通常称为身份验证系统，因为这些功能在某种程度上是耦合的。

## `User` 对象

用户对象是认证系统的核心。它通常代表了与你的站点交互的人员，并用于允许诸如限制访问、注册用户配置文件、将内容与创建者关联等功能。Django 的认证框架中用户只有一个类，例如 “超级管理员”或“普通管理员”只是具有特殊属性集的用户对象，而不是用户对象的不同类。

默认用户的主要属性是：

- [`username`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.User.username)
- [`password`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.User.password)
- [`email`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.User.email)
- [`first_name`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.User.first_name)
- [`last_name`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.User.last_name)

请参阅完整的API文档 [`full API documentation`](/zh-hans/2.1/ref/contrib/auth/#id1) 以获得完整的参考，下面的文档主要以任务为导向。

### 创建用户

创建用户最直接的方法是使用包含 [`create_user()`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.UserManager.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\> 。

### 创建超级用户

通过命令行 [`createsuperuser`](/zh-hans/2.1/ref/django-admin/#django-admin-createsuperuser) 创建超级管理员：

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

你将会被提示输入密码，完成之后，超级管理员就被创建成功了。如果你没有填写参数 ``--username <createsuperuser --username> ` or :option:``--email \<createsuperuser --email\>\` ，也将会被提示输入这些值。

### 更改密码

Django 不会在用户模型里保存原始(明文)密码，而只会存储哈希值(请参阅文档 如何管理密码  [documentation of how passwords are managed](/zh-hans/2.1/topics/auth/passwords/) ) 。因此，请不要试图直接操作用户的密码，这就是创建用户需要辅助函数的原因。

更改一个用户的密码，你有几个选择：

[`manage.py changepassword *username*`](/zh-hans/2.1/ref/django-admin/#django-admin-changepassword) 提供了在命令行修改用户密码的方法。它会提示你输入两次新密码，如果操作成功，新密码就立刻生效。如果你没有提供参数 username ，那么将会尝试修改当前系统用户的密码。

你也可以在代码里修改密码，使用 [`set_password()`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.User.set_password):

```pycon
>>> 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](#session-invalidation-on-password-change) 。

### 验证用户

#### `authenticate(request=None, \**credentials)`

使用 [`authenticate()`](#django.contrib.auth.authenticate) 来验证用户。它使用 `username` 和 `password` 作为参数来验证，对每个身份验证后端( authentication backend \` )进行检查。如果后端验证有效，则返回一个 :class:\`~django.contrib.auth.models.User 对象。如果后端引发 [`PermissionDenied`](/zh-hans/2.1/ref/exceptions/#django.core.exceptions.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 credentials
```

`request` 是可选的 [`HttpRequest`](/zh-hans/2.1/ref/request-response/#django.http.HttpRequest) ，它在身份验证后端上的 `authenticate()` 方法来传递。

> **Note**
>
> 这个一个很底层的验证方法。比如，可以通过 [`RemoteUserMiddleware`](/zh-hans/2.1/ref/middleware/#django.contrib.auth.middleware.RemoteUserMiddleware) 来验证。除非你在编写自己的身份验证系统，否则你可能不会用到它。如果你正在寻找用户登录的方法，请参阅 [`LoginView`](#django.contrib.auth.views.LoginView) 。

## 权限和认证

Django 带有一个简单的权限系统。它提供了为指定的用户和用户组分配权限的方法。

它在 Django 管理后台界面里使用，但你也可以在自己的代码中使用它。

Django 的 admin 页面使用了如下权限：

- 访问查看的对象仅限于具有该类型对象的“查看”或“更改”权限的用户。
- 访问“添加”表单和添加对象仅限于具有该类型对象的“添加”权限的用户。
- 访问修改列表、查看“修改”表单和修改对象仅限于对该类型对象的“修改”权限的用户。
- 访问删除对象仅限于对该类型对象的“删除”权限的用户。

不仅可以为每个对象类型设置权限，还可以为每个指定对象实例设置权限。通过使用 [`ModelAdmin`](/zh-hans/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin) 类提供的 [`has_view_permission()`](/zh-hans/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.has_view_permission), [`has_add_permission()`](/zh-hans/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.has_add_permission), [`has_change_permission()`](/zh-hans/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.has_change_permission) 和 [`has_delete_permission()`](/zh-hans/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.has_delete_permission) 方法，可以为同一类型的不同实例定制权限。

[`User`](/zh-hans/2.1/ref/contrib/auth/#id1) 对象有两个多对多字段:`groups` 和 `user_permissions`。 [`User`](/zh-hans/2.1/ref/contrib/auth/#id1) 对象可以像访问其他 :doc:Django model : 一样访问他们的相关对象。

```
myuser.groups.set([group_list])
myuser.groups.add(group, group, ...)
myuser.groups.remove(group, group, ...)
myuser.groups.clear()
myuser.user_permissions.set([permission_list])
myuser.user_permissions.add(permission, permission, ...)
myuser.user_permissions.remove(permission, permission, ...)
myuser.user_permissions.clear()
```

### 默认权限

当 [`INSTALLED_APPS`](/zh-hans/2.1/ref/settings/#std-setting-INSTALLED_APPS) 设置了 `django.contrib.auth` 时，它将确保你的每个 Django 模型被创建时有四个默认权限：添加、修改、删除和查看。

运行 [`manage.py migrate`](/zh-hans/2.1/ref/django-admin/#django-admin-migrate) 时将创建这些权限。当你添加 `django.contrib.auth` 到 [`INSTALLED_APPS`](/zh-hans/2.1/ref/settings/#std-setting-INSTALLED_APPS) 后第一次运行 `迁移` ，将会为所有只去已经安装过的模型以及现在正在安装的模型创建这些默认的权限。之后，每次你运行 [`manage.py migrate`](/zh-hans/2.1/ref/django-admin/#django-admin-migrate) 都会为新模型创建默认权限 (创建权限的函数连接 [`post_migrate`](/zh-hans/2.1/ref/signals/#django.db.models.signals.post_migrate) 信号)。

假设你有一个名为 `foo` 应用程序和一个名为 `Bar` 的模型，要测试基础权限，你应该使用：

- 添加：`user.has_perm('foo.add_bar')`
- 修改：`user.has_perm('foo.change_bar')`
- 删除：`user.has_perm('foo.delete_bar')`
- 查看：`user.has_perm('foo.view_bar')`

权限模型很少会被直接访问。

### 组

[`django.contrib.auth.models.Group`](/zh-hans/2.1/ref/contrib/auth/#id6) 模型是对用户进行分类的通用方法，因此您可以将权限或其他标签应用于这些用户。用户可以属于任意数量的组。

组里的用户会自动拥有该组的权限。举例，如果 `Site editors` 组有修改网站首页的权限，那么该组的任何成员都有这个权限。

除权限外，组是一个方便的途径，可以给用户分类，为其提供一些标签或扩展功能。例如，你可以创建一个组 `'Special users'`，并在编写的代码里让该组成员访问网站仅限会员部分的内容，或者对该组成员发送仅限会员查看的电子邮件。

### 以编程方式创建权限

虽然可以在模型的  `Meta` 类中定义 [custom permissions](/zh-hans/2.1/topics/auth/customizing/#custom-permissions) ，你也可以直接创建权限。例如，你可以为 `BlogPost` 模型创建 `can_publish` 权限。

```
from myapp.models import BlogPost
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType

content_type = ContentType.objects.get_for_model(BlogPost)
permission = Permission.objects.create(
    codename='can_publish',
    name='Can Publish Posts',
    content_type=content_type,
)
```

然后，可以通过 `user_permissions` 属性将权限分配给 [`User`](/zh-hans/2.1/ref/contrib/auth/#id1) ，或通过 `permissions` 属性分配给 [`Group`](/zh-hans/2.1/ref/contrib/auth/#id6)  。

### 权限缓存

在第一次需要获取用户对象的权限检查时， [`ModelBackend`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.backends.ModelBackend) 才会缓存它们的权限。对于请求-响应周期来说，这通常是很好的，因为权限通常不会在添加的时候立刻检查（例如，在 admin 中）。如果你打算在测试或视图中添加权限，并随后检查他们，最简单的解决方案就是从数据库中重新获取用户。例如：

```
from django.contrib.auth.models import Permission, User
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import get_object_or_404

from myapp.models import BlogPost

def user_gains_perms(request, user_id):
    user = get_object_or_404(User, pk=user_id)
    # any permission check will cache the current set of permissions
    user.has_perm('myapp.change_blogpost')

    content_type = ContentType.objects.get_for_model(BlogPost)
    permission = Permission.objects.get(
        codename='change_blogpost',
        content_type=content_type,
    )
    user.user_permissions.add(permission)

    # Checking the cached permission set
    user.has_perm('myapp.change_blogpost')  # False

    # Request new instance of User
    # Be aware that user.refresh_from_db() won't clear the cache.
    user = get_object_or_404(User, pk=user_id)

    # Permission cache is repopulated from the database
    user.has_perm('myapp.change_blogpost')  # True

    ...
```

## Web 请求的认证

Django 使用 [sessions](/zh-hans/2.1/topics/http/sessions/) 和中间件将身份验证系统挂接到请求对象中。

它们在每次请求中都会提供 [`request.user`](/zh-hans/2.1/ref/request-response/#django.http.HttpRequest.user) 属性。如果当前没有用户登录，这个属性将会被设置为 [`AnonymousUser`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.AnonymousUser) ，否则将会被设置为 [`User`](/zh-hans/2.1/ref/contrib/auth/#id1) 实例。

你可以使用 [`is_authenticated`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.User.is_authenticated) 区分两者，例如：

```
if request.user.is_authenticated:
    # Do something for authenticated users.
    ...
else:
    # Do something for anonymous users.
    ...
```

### 用户如何登陆

如果有一个已验证的用户想附加到当前会话(session)中，将通过 [`login()`](#django.contrib.auth.login)  函数完成。

#### `login(request, user, backend=None)`

要在视图中让用户登录，使用 [`login()`](#django.contrib.auth.login)  。它需要 [`HttpRequest`](/zh-hans/2.1/ref/request-response/#django.http.HttpRequest) 对象和 [`User`](/zh-hans/2.1/ref/contrib/auth/#id1) 对象。通过 Django 的 session 框架， [`login()`](#django.contrib.auth.login) 会在 session 中保存用户的ID。

注意，在匿名会话期间设置的任何数据都会在用户登录后保留在会话中。

这个例子展示了如何使用 [`authenticate()`](#django.contrib.auth.authenticate) 和 [`login()`](#django.contrib.auth.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.
        ...
```

#### 选择验证后端

当用户登录时，用户 ID 和用于身份验证的后端会被保存在用户会话中。允许相同的 [authentication backend](/zh-hans/2.1/topics/auth/customizing/#authentication-backends) 在未来的请求中获取用户详情。选择要在会话中保存的验证后端如下：

1. 使用提供了的可选 `backend` 参数值。
2. 使用 `user.backend`  的值。允许配对 [`authenticate()`](#django.contrib.auth.authenticate)  和 [`login()`](#django.contrib.auth.login) ：当返回用户对象时 [`authenticate()`](#django.contrib.auth.authenticate)  设置 `user.backend` 属性。
3. 使用 [`AUTHENTICATION_BACKENDS`](/zh-hans/2.1/ref/settings/#std-setting-AUTHENTICATION_BACKENDS) 存在的 `backend` 。
4. 否则，抛出一个异常。

在1和2中，`backend` 参数和 `user.backend` 属性应该是完整的导入路径（像 [`AUTHENTICATION_BACKENDS`](/zh-hans/2.1/ref/settings/#std-setting-AUTHENTICATION_BACKENDS) 里的路径一样），而不是真实的后端类。

### 用户如何登出

#### `logout(request)`

如果已经通过 [`django.contrib.auth.login()`](#django.contrib.auth.login) 登录的用户想退出登录，可以在视图中使用 [`django.contrib.auth.logout()`](#django.contrib.auth.logout) 。需要传入 [`HttpRequest`](/zh-hans/2.1/ref/request-response/#django.http.HttpRequest) 对象，并且该函数不会返回值。例如：

```
from django.contrib.auth import logout

def logout_view(request):
    logout(request)
    # Redirect to a success page.
```

注意，如果用户未登录，[`logout()`](#django.contrib.auth.logout) 不会报错。

调用 [`logout()`](#django.contrib.auth.logout) 后，当前请求的会话数据会被全部清除。这是为了防止其他使用同一个浏览器的用户访问前一名用户的会话数据。如果想在登出后立即向用户提供的会话中放入任何内容，请在调用 [`django.contrib.auth.logout()`](#django.contrib.auth.logout) 之后执行此操作。

### 限制对登录用户的访问

#### 原始方式

限制访问页面最简单的办法就是检查 [`request.user.is_authenticated`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.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` 装饰器

#### `login_required(redirect_field_name='next', login_url=None)`

作为快捷方式，你可以使用 [`login_required()`](#django.contrib.auth.decorators.login_required) 装饰器:

```
from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    ...
```

[`login_required()`](#django.contrib.auth.decorators.login_required) 会执行以下操作：

- 如果用户没有登录，会重定向到  [`settings.LOGIN_URL`](/zh-hans/2.1/ref/settings/#std-setting-LOGIN_URL) ，并传递绝对路径到查询字符串中。例如： `/accounts/login/?next=/polls/3/` 。
- 如果用户已经登录，则正常执行视图。视图里的代码可以假设用户已经登录了。

默认情况下，成功验证时用户跳转的路径保存在名为 `"next"` 的查询字符串参数中。如果你希望这个参数使用不同名称，请在 [`login_required()`](#django.contrib.auth.decorators.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()`](#django.contrib.auth.decorators.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`](/zh-hans/2.1/ref/settings/#std-setting-LOGIN_URL)  和登录视图是正确关联的。例如，使用默认方式，在 URL 配置文件里添加下面这行：

```
from django.contrib.auth import views as auth_views

path('accounts/login/', auth_views.LoginView.as_view()),
```

[`settings.LOGIN_URL`](/zh-hans/2.1/ref/settings/#std-setting-LOGIN_URL) 也接受视图方法名和 [named URL patterns](/zh-hans/2.1/topics/http/urls/#naming-url-patterns) 。这样你可以在 URLconf 里自由地重新映射你的登录视图，而不需更新配置文件。

> **Note**
>
> `login_required` 装饰器不会检查用户的 `is_active` 标识状态，但默认的 [`AUTHENTICATION_BACKENDS`](/zh-hans/2.1/ref/settings/#std-setting-AUTHENTICATION_BACKENDS) 会拒绝非正常用户。

> **See also**
>
> 如果你打算编写自定义的 Django 管理模块视图（或需要与内置视图使用同样的权限检查），你将会发现 [`django.contrib.admin.views.decorators.staff_member_required()`](/zh-hans/2.1/ref/contrib/admin/#django.contrib.admin.views.decorators.staff_member_required) 装饰器是 `login_required()` 的一个有用的替代方法。

#### `LoginRequired` Mixin

使用基于类的视图时，可以使用 `LoginRequiredMixin` 实现和 `login_required` 相同的行为。这个 Mixin 应该在继承列表中最左侧的位置。

#### `class LoginRequiredMixin`

如果一个视图使用 Mixin ，那么未经验证用户的所有请求都会被重定向到登录页面或者显示 HTTP 403 Forbidden 错误，这取决于 [`raise_exception`](#django.contrib.auth.mixins.AccessMixin.raise_exception) 参数。

你可以设置 [`AccessMixin`](#django.contrib.auth.mixins.AccessMixin) 的任何参数来自定义未验证用户的处理：

```
from django.contrib.auth.mixins import LoginRequiredMixin

class MyView(LoginRequiredMixin, View):
    login_url = '/login/'
    redirect_field_name = 'redirect_to'
```

> **Note**
>
> 同 `login_required` 装饰器一样，Mixin 不会检查用户的 `is_active` 标识状态，但默认的 [`AUTHENTICATION_BACKENDS`](/zh-hans/2.1/ref/settings/#std-setting-AUTHENTICATION_BACKENDS) 会拒绝非正常用户。

#### 限制对通过测试的登录用户的访问

根据某些权限或者其他测试来限制访问，你基本上可以执行和上一节所述同样的操作。

最简单的方法是在视图里直接对 [`request.user`](/zh-hans/2.1/ref/request-response/#django.http.HttpRequest.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')`

作为快捷方式，你可以方便的调用 `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()`](#django.contrib.auth.decorators.user_passes_test) 接受一个必要的参数：一个带有:class:~django.contrib.auth.models.User 对象的调用，如果允许用户访问这个页面，则返回 `True` 。注意，[`user_passes_test()`](#django.contrib.auth.decorators.user_passes_test) 不会自动检查用户是否匿名。

[`user_passes_test()`](#django.contrib.auth.decorators.user_passes_test) 可以传递两个可选参数：

**`login_url`**

  允许你指定用户没有通过测试时跳转的地址。它可能是一个登录页面，如果你没指定，默认是 [`settings.LOGIN_URL`](/zh-hans/2.1/ref/settings/#std-setting-LOGIN_URL) 。

**`redirect_field_name`**

  与 [`login_required()`](#django.contrib.auth.decorators.login_required) 相同。如果你想把没通过检查的用户重定向到没有 "next page" 的非登录页面时，把它设置为 `None` ，这样它会在 URL 中移除。

例如:

```
@user_passes_test(email_check, login_url='/login/')
def my_view(request):
    ...
```

#### `class UserPassesTestMixin`

使用基于类的视图时，可以使用 `UserPassesTestMixin` 执行此操作。

#### `test_func()`

你必须覆盖类方法 `test_func()` 以提供执行的测试。此外，还可以设置 [`AccessMixin`](#django.contrib.auth.mixins.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()`

你也可以覆盖 `get_test_func()` 方法，以使 mixin 对其检查使用不同名称的函数（而不是 [`test_func()`](#django.contrib.auth.mixins.UserPassesTestMixin.test_func) ）。

> **Stacking UserPassesTestMixin**
>
> 由于实现了 `UserPassesTestMixin` 方式，不能在继承列表中堆砌它们。下述方式将不能工作：
>
> ```
> class TestMixin1(UserPassesTestMixin):
>     def test_func(self):
>         return self.request.user.email.endswith('@example.com')
>
> class TestMixin2(UserPassesTestMixin):
>     def test_func(self):
>         return self.request.user.username.startswith('django')
>
> class MyView(TestMixin1, TestMixin2, View):
>     ...
> ```
>
> 如果 `TestMixin1` 调用 `super()` 并把结果考虑在内，`TestMixin1` 将不能独立运行。

#### `permission_required` 装饰器

#### `permission_required(perm, login_url=None, raise_exception=False)`

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()`](#django.contrib.auth.decorators.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()`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.User.has_perm) method,
permission names take the form `"<app label>.<permission codename>"`
(i.e. `polls.can_vote` for a permission on a model in the `polls`
application).

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()`](#django.contrib.auth.decorators.permission_required)
also takes an optional `login_url` parameter:

```
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()`](#django.contrib.auth.decorators.login_required) decorator,
`login_url` defaults to [`settings.LOGIN_URL`](/zh-hans/2.1/ref/settings/#std-setting-LOGIN_URL).

If the `raise_exception` parameter is given, the decorator will raise
[`PermissionDenied`](/zh-hans/2.1/ref/exceptions/#django.core.exceptions.PermissionDenied), prompting [the 403
(HTTP Forbidden) view](/zh-hans/2.1/ref/views/#http-forbidden-view) instead of redirecting to the
login page.

If you want to use `raise_exception` but also give your users a chance to
login first, you can add the
[`login_required()`](#django.contrib.auth.decorators.login_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`](#django.contrib.auth.views.LoginView)'s
`redirect_authenticated_user=True` and the logged-in user doesn't have
all of the required permissions.

#### The `PermissionRequiredMixin` mixin

To apply permission checks to [class-based views](/zh-hans/2.1/ref/class-based-views/), you can use the `PermissionRequiredMixin`:

#### `class PermissionRequiredMixin`

This mixin, just like the `permission_required`
decorator, checks whether the user accessing a view has all given
permissions. You should specify the permission (or an iterable of
permissions) using the `permission_required` parameter:

```
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
[`AccessMixin`](#django.contrib.auth.mixins.AccessMixin) to customize the handling
of unauthorized users.

你可能同样需要重写这些方法：

#### `get_permission_required()`

Returns an iterable of permission names used by the mixin. Defaults to
the `permission_required` attribute, converted to a tuple if
necessary.

#### `has_permission()`

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()`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.User.has_perms) with the
list of permissions returned by [`get_permission_required()`](#django.contrib.auth.mixins.PermissionRequiredMixin.get_permission_required).

### Redirecting unauthorized requests in class-based views

To ease the handling of access restrictions in [class-based views](/zh-hans/2.1/ref/class-based-views/), the `AccessMixin` can be used to configure
the behavior of a view when access is denied. Authenticated users are denied
access with an HTTP 403 Forbidden response. Anonymous users are redirected to
the login page or shown an HTTP 403 Forbidden response, depending on the
[`raise_exception`](#django.contrib.auth.mixins.AccessMixin.raise_exception) attribute.

> **Changed in Django 2.1**
>
> In older versions, authenticated users who lacked permissions were
> redirected to the login page (which resulted in a loop) instead of
> receiving an HTTP 403 Forbidden response.

#### `class AccessMixin`

#### `login_url`

Default return value for [`get_login_url()`](#django.contrib.auth.mixins.AccessMixin.get_login_url).  Defaults to `None`
in which case [`get_login_url()`](#django.contrib.auth.mixins.AccessMixin.get_login_url) falls back to
[`settings.LOGIN_URL`](/zh-hans/2.1/ref/settings/#std-setting-LOGIN_URL).

#### `permission_denied_message`

Default return value for [`get_permission_denied_message()`](#django.contrib.auth.mixins.AccessMixin.get_permission_denied_message).
Defaults to an empty string.

#### `redirect_field_name`

Default return value for [`get_redirect_field_name()`](#django.contrib.auth.mixins.AccessMixin.get_redirect_field_name). Defaults to
`"next"`.

#### `raise_exception`

If this attribute is set to `True`, a
[`PermissionDenied`](/zh-hans/2.1/ref/exceptions/#django.core.exceptions.PermissionDenied) exception is raised
when the conditions are not met.  When `False` (the default),
anonymous users are redirected to the login page.

#### `get_login_url()`

Returns the URL that users who don't pass the test will be redirected
to. Returns [`login_url`](#django.contrib.auth.mixins.AccessMixin.login_url) if set, or [`settings.LOGIN_URL`](/zh-hans/2.1/ref/settings/#std-setting-LOGIN_URL) otherwise.

#### `get_permission_denied_message()`

When [`raise_exception`](#django.contrib.auth.mixins.AccessMixin.raise_exception) is `True`, this method can be used to
control the error message passed to the error handler for display to
the user. Returns the [`permission_denied_message`](#django.contrib.auth.mixins.AccessMixin.permission_denied_message) attribute by
default.

#### `get_redirect_field_name()`

Returns the name of the query parameter that will contain the URL the
user should be redirected to after a successful login. If you set this
to `None`, a query parameter won't be added. Returns the
[`redirect_field_name`](#django.contrib.auth.mixins.AccessMixin.redirect_field_name) attribute by default.

#### `handle_no_permission()`

Depending on the value of `raise_exception`, the method either raises
a [`PermissionDenied`](/zh-hans/2.1/ref/exceptions/#django.core.exceptions.PermissionDenied) exception or
redirects the user to the `login_url`, optionally including the
`redirect_field_name` if it is set.

#### Session invalidation on password change

If your [`AUTH_USER_MODEL`](/zh-hans/2.1/ref/settings/#std-setting-AUTH_USER_MODEL) inherits from
[`AbstractBaseUser`](/zh-hans/2.1/topics/auth/customizing/#django.contrib.auth.models.AbstractBaseUser) or implements its own
[`get_session_auth_hash()`](/zh-hans/2.1/topics/auth/customizing/#django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash)
method, authenticated sessions will include the hash returned by this function.
In the [`AbstractBaseUser`](/zh-hans/2.1/topics/auth/customizing/#django.contrib.auth.models.AbstractBaseUser) case, this is an
HMAC of the password field. Django verifies that the hash in the session for
each request matches the one that's computed during the request. This allows a
user to log out all of their sessions by changing their password.

The default password change views included with Django,
[`PasswordChangeView`](#django.contrib.auth.views.PasswordChangeView) and the
`user_change_password` view in the [`django.contrib.auth`](/zh-hans/2.1/topics/auth/#module-django.contrib.auth) admin, update
the session with the new password hash so that a user changing their own
password won't log themselves out. If you have a custom password change view
and wish to have similar behavior, use the [`update_session_auth_hash()`](#django.contrib.auth.update_session_auth_hash)
function.

#### `update_session_auth_hash(request, user)`

This function takes the current request and the updated user object from
which the new session hash will be derived and updates the session hash
appropriately. It also rotates the session key so that a stolen session
cookie will be invalidated.

Example usage:

```
from django.contrib.auth import update_session_auth_hash

def password_change(request):
    if request.method == 'POST':
        form = PasswordChangeForm(user=request.user, data=request.POST)
        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
    else:
        ...
```

> **Note**
>
> Since
> [`get_session_auth_hash()`](/zh-hans/2.1/topics/auth/customizing/#django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash)
> is based on [`SECRET_KEY`](/zh-hans/2.1/ref/settings/#std-setting-SECRET_KEY), updating your site to use a new secret
> will invalidate all existing sessions.

### Authentication Views

Django provides several views that you can use for handling login, logout, and
password management. These make use of the [stock auth forms](#built-in-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](#all-authentication-views).

#### Using the views

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](/zh-hans/2.1/topics/http/urls/) 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](/zh-hans/2.1/topics/class-based-views/), which allows
you to easily customize them by subclassing.

#### All authentication views

This is a list with all the views `django.contrib.auth` provides. For
implementation details see [Using the views](#using-the-views).

#### `class LoginView`

**URL name:** `login`

See [the URL documentation](/zh-hans/2.1/topics/http/urls/) 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 to `registration/login.html`.
- `redirect_field_name`: The name of a `GET` field containing the
  URL to redirect to after login. Defaults to `next`.
- `authentication_form`: A callable (typically just a form class) to
  use for authentication. Defaults to
  [`AuthenticationForm`](#django.contrib.auth.forms.AuthenticationForm).
- `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 to `False`.

  > **Warning**
  >
  > If you enable `redirect_authenticated_user`, other websites will be
  > able to determine if their visitors are authenticated on your site by
  > requesting redirect URLs to image files on your website. To avoid
  > this "[social media fingerprinting](https://robinlinus.github.io/socialmedia-leak/)" information
  > leakage, host all images and your favicon on a separate domain.
  >
  > Enabling `redirect_authenticated_user` can also result in a redirect
  > loop when using the [`permission_required()`](#django.contrib.auth.decorators.permission_required) decorator
  > unless the `raise_exception` parameter is used.
- `success_url_allowed_hosts`: A [`set`](https://docs.python.org/3/library/stdtypes.html#set) of hosts, in addition to
  [`request.get_host()`](/zh-hans/2.1/ref/request-response/#django.http.HttpRequest.get_host), that are
  safe for redirecting after login. Defaults to an empty [`set`](https://docs.python.org/3/library/stdtypes.html#set).

Here's what `LoginView` does:

- If called via `GET`, it displays a login form that POSTs to the
  same URL. More on this in a bit.
- If called via `POST` with user submitted credentials, it tries to log
  the user in. If login is successful, the view redirects to the URL
  specified in `next`. If `next` isn't provided, it redirects to
  [`settings.LOGIN_REDIRECT_URL`](/zh-hans/2.1/ref/settings/#std-setting-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.html` by default. This template gets passed
four template context variables:

- `form`: A [`Form`](/zh-hans/2.1/ref/forms/api/#django.forms.Form) object representing the
  [`AuthenticationForm`](#django.contrib.auth.forms.AuthenticationForm).
- `next`: The URL to redirect to after successful login. This may
  contain a query string, too.
- `site`: The current [`Site`](/zh-hans/2.1/ref/contrib/sites/#django.contrib.sites.models.Site),
  according to the [`SITE_ID`](/zh-hans/2.1/ref/settings/#std-setting-SITE_ID) setting. If you don't have the
  site framework installed, this will be set to an instance of
  [`RequestSite`](/zh-hans/2.1/ref/contrib/sites/#django.contrib.sites.requests.RequestSite), which derives the
  site name and domain from the current
  [`HttpRequest`](/zh-hans/2.1/ref/request-response/#django.http.HttpRequest).
- `site_name`: An alias for `site.name`. If you don't have the site
  framework installed, this will be set to the value of
  [`request.META['SERVER_NAME']`](/zh-hans/2.1/ref/request-response/#django.http.HttpRequest.META).
  For more on sites, see [The "sites" framework](/zh-hans/2.1/ref/contrib/sites/).

If you'd prefer not to call the template `registration/login.html`,
you can pass the `template_name` parameter via the extra arguments to
the `as_view` method in your URLconf. For example, this URLconf line would
use `myapp/login.html` instead:

```
path('accounts/login/', auth_views.LoginView.as_view(template_name='myapp/login.html')),
```

You can also specify the name of the `GET` field which contains the URL
to redirect to after login using `redirect_field_name`. By default, the
field is called `next`.

Here's a sample `registration/login.html` template you can use as a
starting point. It assumes you have a `base.html` template that
defines a `content` block:

```html+django
{% 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](/zh-hans/2.1/topics/auth/customizing/)) you can use a custom authentication form by
setting the `authentication_form` attribute. This form must accept a
`request` keyword argument in its `__init__()` method and provide a
`get_user()` method which returns the authenticated user object (this
method is only ever called after successful form validation).

#### `class LogoutView`

Logs a user out.

**URL name:** `logout`

**Attributes:**

- `next_page`: The URL to redirect to after logout. Defaults to
  [`settings.LOGOUT_REDIRECT_URL`](/zh-hans/2.1/ref/settings/#std-setting-LOGOUT_REDIRECT_URL).
- `template_name`: The full name of a template to display after
  logging the user out. Defaults to `registration/logged_out.html`.
- `redirect_field_name`: The name of a `GET` field containing the
  URL to redirect to after log out. Defaults to `next`. Overrides the
  `next_page` URL if the given `GET` parameter 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`: A [`set`](https://docs.python.org/3/library/stdtypes.html#set) of hosts, in addition to
  [`request.get_host()`](/zh-hans/2.1/ref/request-response/#django.http.HttpRequest.get_host), that are
  safe for redirecting after logout. Defaults to an empty [`set`](https://docs.python.org/3/library/stdtypes.html#set).

**Template context:**

- `title`: The string "Logged out", localized.
- `site`: The current [`Site`](/zh-hans/2.1/ref/contrib/sites/#django.contrib.sites.models.Site),
  according to the [`SITE_ID`](/zh-hans/2.1/ref/settings/#std-setting-SITE_ID) setting. If you don't have the
  site framework installed, this will be set to an instance of
  [`RequestSite`](/zh-hans/2.1/ref/contrib/sites/#django.contrib.sites.requests.RequestSite), which derives the
  site name and domain from the current
  [`HttpRequest`](/zh-hans/2.1/ref/request-response/#django.http.HttpRequest).
- `site_name`: An alias for `site.name`. If you don't have the site
  framework installed, this will be set to the value of
  [`request.META['SERVER_NAME']`](/zh-hans/2.1/ref/request-response/#django.http.HttpRequest.META).
  For more on sites, see [The "sites" framework](/zh-hans/2.1/ref/contrib/sites/).

#### `logout_then_login(request, login_url=None)`

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 to [`settings.LOGIN_URL`](/zh-hans/2.1/ref/settings/#std-setting-LOGIN_URL) if not supplied.

#### `class PasswordChangeView`

**URL name:** `password_change`

Allows a user to change their password.

**Attributes:**

- `template_name`: The full name of a template to use for
  displaying the password change form. Defaults to
  `registration/password_change_form.html` if not supplied.
- `success_url`: The URL to redirect to after a successful password
  change.
- `form_class`: A custom "change password" form which must accept a
  `user` keyword argument. The form is responsible for actually changing
  the user's password. Defaults to
  [`PasswordChangeForm`](#django.contrib.auth.forms.PasswordChangeForm).
- `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 (see `form_class` above).

#### `class PasswordChangeDoneView`

**URL name:** `password_change_done`

The page shown after a user has changed their password.

**Attributes:**

- `template_name`: The full name of a template to use.
  Defaults to `registration/password_change_done.html` if not
  supplied.
- `extra_context`: A dictionary of context data that will be added to the
  default context data passed to the template.

#### `class PasswordResetView`

**URL name:** `password_reset`

Allows 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
[`PasswordResetForm`](#django.contrib.auth.forms.PasswordResetForm) and use the
`form_class` attribute.

Users flagged with an unusable password (see
[`set_unusable_password()`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.User.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 to
  `registration/password_reset_form.html` if not supplied.
- `form_class`: Form that will be used to get the email of
  the user to reset the password for. Defaults to
  [`PasswordResetForm`](#django.contrib.auth.forms.PasswordResetForm).
- `email_template_name`: The full name of a template to use for
  generating the email with the reset password link. Defaults to
  `registration/password_reset_email.html` if 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
  to `registration/password_reset_subject.txt` if not supplied.
- `token_generator`: Instance of the class to check the one time link.
  This will default to `default_token_generator`, it's an instance of
  `django.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
  the [`DEFAULT_FROM_EMAIL`](/zh-hans/2.1/ref/settings/#std-setting-DEFAULT_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 a `text/html` multipart 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 (see `form_class` above) for resetting the user's
  password.

**Email template context:**

- `email`: An alias for `user.email`
- `user`: The current [`User`](/zh-hans/2.1/ref/contrib/auth/#id1),
  according to the `email` form field. Only active users are able to
  reset their passwords (`User.is_active is True`).
- `site_name`: An alias for `site.name`. If you don't have the site
  framework installed, this will be set to the value of
  [`request.META['SERVER_NAME']`](/zh-hans/2.1/ref/request-response/#django.http.HttpRequest.META).
  For more on sites, see [The "sites" framework](/zh-hans/2.1/ref/contrib/sites/).
- `domain`: An alias for `site.domain`. If you don't have the site
  framework installed, this will be set to the value of
  `request.get_host()`.
- `protocol`: http or https
- `uid`: 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):

```html+django
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 PasswordResetDoneView`

**URL name:** `password_reset_done`

The page shown after a user has been emailed a link to reset their
password. This view is called by default if the [`PasswordResetView`](#django.contrib.auth.views.PasswordResetView)
doesn't have an explicit `success_url` URL set.

> **Note**
>
> If the email address provided does not exist in the system, the user is
> inactive, or has an unusable password, the user will still be
> redirected to this view but no email will be sent.

**Attributes:**

- `template_name`: The full name of a template to use.
  Defaults to `registration/password_reset_done.html` if not
  supplied.
- `extra_context`: A dictionary of context data that will be added to the
  default context data passed to the template.

#### `class PasswordResetConfirmView`

**URL name:** `password_reset_confirm`

Presents 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 is
  `registration/password_reset_confirm.html`.
- `token_generator`: Instance of the class to check the password. This
  will default to `default_token_generator`, it's an instance of
  `django.contrib.auth.tokens.PasswordResetTokenGenerator`.
- `post_reset_login`: A boolean indicating if the user should be
  automatically authenticated after a successful password reset. Defaults
  to `False`.
- `post_reset_login_backend`: A dotted path to the authentication
  backend to use when authenticating a user if `post_reset_login` is
  `True`. Required only if you have multiple
  [`AUTHENTICATION_BACKENDS`](/zh-hans/2.1/ref/settings/#std-setting-AUTHENTICATION_BACKENDS) configured. Defaults to `None`.
- `form_class`: Form that will be used to set the password. Defaults to
  [`SetPasswordForm`](#django.contrib.auth.forms.SetPasswordForm).
- `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 (see `form_class` above) for setting the new user's
  password.
- `validlink`: Boolean, True if the link (combination of `uidb64` and
  `token`) is valid or unused yet.

#### `class PasswordResetCompleteView`

**URL name:** `password_reset_complete`

Presents 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 to `registration/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 functions

#### `redirect_to_login(next, login_url=None, redirect_field_name='next')`

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 to [`settings.LOGIN_URL`](/zh-hans/2.1/ref/settings/#std-setting-LOGIN_URL) if not supplied.
- `redirect_field_name`: The name of a `GET` field containing the
  URL to redirect to after log out. Overrides `next` if the given
  `GET` parameter is passed.

### Built-in forms

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`](#module-django.contrib.auth.forms):

> **Note**
>
> The built-in authentication forms make certain assumptions about the user
> model that they are working with. If you're using a [custom user model](/zh-hans/2.1/topics/auth/customizing/#auth-custom-user), it may be necessary to define your own forms for the
> authentication system. For more information, refer to the documentation
> about [using the built-in authentication forms with custom user models](/zh-hans/2.1/topics/auth/customizing/#custom-users-and-the-built-in-auth-forms).

#### `class AdminPasswordChangeForm`

A form used in the admin interface to change a user's password.

Takes the `user` as the first positional argument.

#### `class AuthenticationForm`

A form for logging a user in.

Takes `request` as its first positional argument, which is stored on the
form instance for use by sub-classes.

#### `confirm_login_allowed(user)`

By default, `AuthenticationForm` rejects users whose `is_active`
flag is set to `False`. You may override this behavior with a custom
policy to determine which users can log in. Do this with a custom form
that subclasses `AuthenticationForm` and overrides the
`confirm_login_allowed()` method. This method should raise a
[`ValidationError`](/zh-hans/2.1/ref/exceptions/#django.core.exceptions.ValidationError) if 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`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.backends.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 PasswordChangeForm`

A form for allowing a user to change their password.

#### `class PasswordResetForm`

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)`

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`, and `html_email_template` (if it is not
  `None`).
- `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 the `context` with the
same variables that
[`PasswordResetView`](#django.contrib.auth.views.PasswordResetView) passes to its
email context.

#### `class SetPasswordForm`

A form that lets a user change their password without entering the old
password.

#### `class UserChangeForm`

A form used in the admin interface to change a user's information and
permissions.

#### `class UserCreationForm`

A [`ModelForm`](/zh-hans/2.1/topics/forms/modelforms/#django.forms.ModelForm) for creating a new user.

It has three fields: `username` (from the user model), `password1`,
and `password2`. It verifies that `password1` and `password2` match,
validates the password using
[`validate_password()`](/zh-hans/2.1/topics/auth/passwords/#django.contrib.auth.password_validation.validate_password), and
sets the user's password using
[`set_password()`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.User.set_password).

### Authentication data in templates

The currently logged-in user and their permissions are made available in the
[template context](/zh-hans/2.1/ref/templates/api/) when you use
[`RequestContext`](/zh-hans/2.1/ref/templates/api/#django.template.RequestContext).

> **Technicality**
>
> Technically, these variables are only made available in the template
> context if you use [`RequestContext`](/zh-hans/2.1/ref/templates/api/#django.template.RequestContext) and the
> `'django.contrib.auth.context_processors.auth'` context processor is
> enabled. It is in the default generated settings file. For more, see the
> [RequestContext docs](/zh-hans/2.1/ref/templates/api/#subclassing-context-requestcontext).

#### Users

When rendering a template [`RequestContext`](/zh-hans/2.1/ref/templates/api/#django.template.RequestContext), the
currently logged-in user, either a  [`User`](/zh-hans/2.1/ref/contrib/auth/#id1)
instance or an [`AnonymousUser`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.AnonymousUser) instance, is
stored in the template variable `{{ user }}`:

```html+django
{% 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.

#### 权限

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()`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.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()`](/zh-hans/2.1/ref/contrib/auth/#django.contrib.auth.models.User.has_perm). For example,
to check if the logged-in user has the permission `foo.can_vote`:

```
{% if perms.foo.can_vote %}
```

以下是一个在模板中检查权限的更完整的示例：

```html+django
{% 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:

```html+django
{% if 'foo' in perms %}
    {% if 'foo.can_vote' in perms %}
        <p>In lookup works, too.</p>
    {% endif %}
{% endif %}
```

## Managing users in the admin

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.

### 创建用户

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!

### 更改密码

User passwords are not displayed in the admin (nor stored in the database), but
the [password storage details](/zh-hans/2.1/topics/auth/passwords/) are displayed.
Included in the display of this information is a link to
a password change form that allows admins to change user passwords.
