---
title: "表单 API"
version: 4.2
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/4.2/ref/forms/api/
canonical: https://djangodocs.dev/zh-hans/4.2/ref/forms/api/
---
# 表单 API

> **关于此文档**
>
> 本文档介绍了 Django 的表单 API 的具体细节。你应该先阅读 [使用表单的介绍](/zh-hans/4.2/topics/forms/)。

## 绑定和非绑定表单

一个 [`Form`](#django.forms.Form) 实例要么是 **绑定** 到一组数据，要么是 **未绑定**。

- 如果是 **绑定** 了一组数据，它就能够验证这些数据，并将表单渲染成 HTML，并在 HTML 中显示数据。
- 如果是 **未绑定**，它就不能进行验证（因为没有数据可验证！），但它仍然可以将空白表单渲染为 HTML。

#### `class Form`

要创建一个未绑定的 [`Form`](#django.forms.Form) 实例，实例化这个类：

```pycon
>>> f = ContactForm()
```

要将数据绑定到表单，将数据作为字典传递给 [`Form`](#django.forms.Form) 类构造函数的第一个参数：

```pycon
>>> data = {
...     "subject": "hello",
...     "message": "Hi there",
...     "sender": "foo@example.com",
...     "cc_myself": True,
... }
>>> f = ContactForm(data)
```

在这个字典中，键是字段名，对应于 [`Form`](#django.forms.Form) 类中的属性。值是你要验证的数据。这些数据通常是字符串，但不要求它们是字符串；你传递的数据类型取决于 [`Field`](/zh-hans/4.2/ref/forms/fields/#django.forms.Field)，我们稍后会看到。

#### `Form.is_bound`

如果在运行时需要区分绑定和未绑定的表单实例，请检查表单的 [`is_bound`](#django.forms.Form.is_bound) 属性的值：

```pycon
>>> f = ContactForm()
>>> f.is_bound
False
>>> f = ContactForm({"subject": "hello"})
>>> f.is_bound
True
```

请注意，传递一个空字典会创建一个带有空数据的 *绑定* 表单：

```pycon
>>> f = ContactForm({})
>>> f.is_bound
True
```

如果你有一个绑定的 [`Form`](#django.forms.Form) 实例，并想以某种方式改变数据，或者你想将一个未绑定的 [`Form`](#django.forms.Form) 实例绑定到一些数据，请创建另一个 [`Form`](#django.forms.Form) 实例。没有办法改变一个 [`Form`](#django.forms.Form) 实例中的数据。一旦创建了一个 [`Form`](#django.forms.Form) 实例，你应该认为它的数据是不可改变的，不管它是否有数据。

## 使用表单来验证数据

#### `Form.clean()`

当你必须为相互依赖的字段添加自定义验证时，在你的 `Form` 上实现一个 `clean()` 方法。参见 [清理和验证相互依赖的字段](/zh-hans/4.2/ref/forms/validation/#validating-fields-with-clean) 的用法示例。

#### `Form.is_valid()`

[`Form`](#django.forms.Form) 对象的主要任务是验证数据。对于一个绑定的 [`Form`](#django.forms.Form) 实例，调用 [`is_valid()`](#django.forms.Form.is_valid) 方法来运行验证，并返回一个布尔值，表示数据是否有效：

```pycon
>>> data = {
...     "subject": "hello",
...     "message": "Hi there",
...     "sender": "foo@example.com",
...     "cc_myself": True,
... }
>>> f = ContactForm(data)
>>> f.is_valid()
True
```

让我们尝试一些无效的数据。在这种情况下，`subject` 是空白的（这是一个错误，因为默认情况下所有字段都是必填的），而 `sender` 不是一个有效的电子邮件地址：

```pycon
>>> data = {
...     "subject": "",
...     "message": "Hi there",
...     "sender": "invalid email address",
...     "cc_myself": True,
... }
>>> f = ContactForm(data)
>>> f.is_valid()
False
```

#### `Form.errors`

访问 [`errors`](#django.forms.Form.errors) 属性以获取错误消息的字典：

```pycon
>>> f.errors
{'sender': ['Enter a valid email address.'], 'subject': ['This field is required.']}
```

在这个字典中，键是字段名，值是代表错误信息的字符串列表。错误信息存储在列表中，因为一个字段可以有多个错误信息。

你可以访问 [`errors`](#django.forms.Form.errors)，而不必先调用 [`is_valid()`](#django.forms.Form.is_valid)。无论是调用 [`is_valid()`](#django.forms.Form.is_valid) 还是访问 [`errors`](#django.forms.Form.errors)，表单的数据都会首先被验证。

验证例程只会被调用一次，无论你访问 [`errors`](#django.forms.Form.errors) 或调用 [`is_valid()`](#django.forms.Form.is_valid) 多少次。这意味着，如果验证有副作用，这些副作用将只被触发一次。

#### `Form.errors.as_data()`

返回一个 `dict`，将字段映射到它们的原始 `ValidationError` 实例。

```
>>> f.errors.as_data()
{'sender': [ValidationError(['Enter a valid email address.'])],
'subject': [ValidationError(['This field is required.'])]}
```

当你需要通过错误 `code` 来识别错误时，请使用此方法。这样就可以在给定的错误出现时，重写错误信息或在视图中编写自定义逻辑。它还可以用来以自定义格式（如 XML）将错误序列化；例如， `as_json()` 依赖于 `as_data()`。

需要使用 `as_data()` 方法是由于向后兼容性。以前，`ValidationError` 实例一旦被添加到 `Form.errors` 字典中，其 **渲染的** 错误信息就会丢失。理想情况下，`Form.errors` 会存储 `ValidationError` 实例，并且带有 `as_` 前缀的方法可以渲染它们，但为了不破坏那些期望在 `Form.errors` 中渲染错误信息的代码，必须反过来做。

#### `Form.errors.as_json(escape_html=False)`

返回以 JSON 格式序列化的错误。

```
>>> f.errors.as_json()
{"sender": [{"message": "Enter a valid email address.", "code": "invalid"}],
"subject": [{"message": "This field is required.", "code": "required"}]}
```

默认情况下，`as_json()` 不会转义其输出。如果你使用它来处理类似 AJAX 请求的表单视图，客户端解释响应并将错误插入到页面中，你会希望确保在客户端转义结果，以避免跨站点脚本攻击的可能性。你可以在 JavaScript 中使用 `element.textContent = errorText` 或者使用 jQuery 的 `$(el).text(errorText)` （而不是它的 `.html()` 函数）来实现。

如果出于某些原因你不想使用客户端转义，你也可以设置 `escape_html=True`，错误信息将被转义，这样你就可以直接在 HTML 中使用它们。

#### `Form.errors.get_json_data(escape_html=False)`

`Form. errors.as_json()` 将返回序列化的 JSON，而这个则是返回序列化之前的错误数据。

`escape_html` 参数的行为如 [`Form.errors.as_json()`](#django.forms.Form.errors.as_json) 中所述。

#### `Form.add_error(field, error)`

该方法允许在 `Form.clean()` 方法中添加错误到特定字段，或者从表单外部添加错误，例如从视图中添加。

`field` 参数是应该添加错误的字段名。如果它的值是 `None`，错误将被视为非字段错误，就像 [`Form.non_field_errors()`](#django.forms.Form.non_field_errors) 返回的那样。

`error` 参数可以是一个字符串，或者最好是 `ValidationError` 的实例。关于定义表单错误的最佳做法，请参见 [引发 ValidationError](/zh-hans/4.2/ref/forms/validation/#raising-validation-error)。

注意，`Form.add_error()` 会自动从 `cleaned_data` 中删除相关字段。

#### `Form.has_error(field, code=None)`

本方法返回一个布尔值，表示一个字段是否有特定错误 `code` 的错误。如果 `code` 是 `None`，如果字段包含任何错误，它将返回 `True`。

要检查非字段错误，使用 [`NON_FIELD_ERRORS`](/zh-hans/4.2/ref/exceptions/#django.core.exceptions.NON_FIELD_ERRORS) 作为 `field` 参数。

#### `Form.non_field_errors()`

这个方法从 [`Form.errors`](#django.forms.Form.errors) 中返回没有与特定字段关联的错误列表。这包括在 [`Form.clean()`](#django.forms.Form.clean) 中引发的 `ValidationError` 和使用 [`Form.add_error(None, "...")`](#django.forms.Form.add_error) 添加的错误。

### 非绑定表单的行为

对于没有数据的表单进行验证是没有意义的，但是为了记录，以下是未绑定表单的情况：

```pycon
>>> f = ContactForm()
>>> f.is_valid()
False
>>> f.errors
{}
```

## 初始表单值

#### `Form.initial`

使用 [`initial`](#django.forms.Form.initial) 在运行时声明表单字段的初始值。例如，你可能想用当前会话的用户名来填写 `username` 字段。

要实现这一点，可以使用 [`initial`](#django.forms.Form.initial) 参数来设置表单的初始值。如果提供了该参数，它应该是一个将字段名称映射到初始值的字典。只需要包括要指定初始值的字段，不需要包括表单中的每个字段。例如：

```pycon
>>> f = ContactForm(initial={"subject": "Hi there!"})
```

这些值只对未绑定的表单显示，如果没有提供特定的值，它们不会被用作后备值。

如果一个 [`Field`](/zh-hans/4.2/ref/forms/fields/#django.forms.Field) 定义了 [`initial`](/zh-hans/4.2/ref/forms/fields/#django.forms.Field.initial)，并且在实例化表单时包括了 [`initial`](#django.forms.Form.initial)，那么后者的 `initial` 会具有优先权。在这个示例中，`initial` 在字段级别和表单实例级别都提供了，后者优先：

```pycon
>>> from django import forms
>>> class CommentForm(forms.Form):
...     name = forms.CharField(initial="class")
...     url = forms.URLField()
...     comment = forms.CharField()
...
>>> f = CommentForm(initial={"name": "instance"}, auto_id=False)
>>> print(f)
<tr><th>Name:</th><td><input type="text" name="name" value="instance" required></td></tr>
<tr><th>Url:</th><td><input type="url" name="url" required></td></tr>
<tr><th>Comment:</th><td><input type="text" name="comment" required></td></tr>
```

#### `Form.get_initial_for_field(field, field_name)`

返回一个表单字段的初始数据。如果存在的话，它从 [`Form.initial`](#django.forms.Form.initial) 检索数据，否则尝试 [`Field.initial`](/zh-hans/4.2/ref/forms/fields/#django.forms.Field.initial)。可调用的值将被执行。

建议使用 [`BoundField.initial`](#django.forms.BoundField.initial) 而不是 [`get_initial_for_field()`](#django.forms.Form.get_initial_for_field)，因为 `BoundField.initial` 具有更简单的接口。而且，与 [`get_initial_for_field()`](#django.forms.Form.get_initial_for_field) 不同，[`BoundField.initial`](#django.forms.BoundField.initial) 缓存其值。这在处理返回值可能会变化的可调用对象时特别有用（例如 `datetime.now` 或 `uuid.uuid4`）：

```pycon
>>> import uuid
>>> class UUIDCommentForm(CommentForm):
...     identifier = forms.UUIDField(initial=uuid.uuid4)
...
>>> f = UUIDCommentForm()
>>> f.get_initial_for_field(f.fields["identifier"], "identifier")
UUID('972ca9e4-7bfe-4f5b-af7d-07b3aa306334')
>>> f.get_initial_for_field(f.fields["identifier"], "identifier")
UUID('1b411fab-844e-4dec-bd4f-e9b0495f04d0')
>>> # Using BoundField.initial, for comparison
>>> f["identifier"].initial
UUID('28a09c59-5f00-4ed9-9179-a3b074fa9c30')
>>> f["identifier"].initial
UUID('28a09c59-5f00-4ed9-9179-a3b074fa9c30')
```

## 检查哪些表格数据已经改变

#### `Form.has_changed()`

当你需要检查表单数据是否与初始数据发生变化时，请使用 `has_changed()` 方法。

```
>>> data = {'subject': 'hello',
...         'message': 'Hi there',
...         'sender': 'foo@example.com',
...         'cc_myself': True}
>>> f = ContactForm(data, initial=data)
>>> f.has_changed()
False
```

当表单提交后，我们会重新构建表单，并提供原始数据，以便进行对比。

```
>>> f = ContactForm(request.POST, initial=data)
>>> f.has_changed()
```

如果来自 `request.POST` 的数据与 [`initial`](#django.forms.Form.initial) 中提供的数据不同，那么 `has_changed()` 将为 `True`，否则为 `False`。结果是通过调用 [`Field.has_changed()`](/zh-hans/4.2/ref/forms/fields/#django.forms.Field.has_changed) 对表单中的每个字段进行计算。

#### `Form.changed_data`

`changed_data` 属性返回表单绑定数据（通常是 `request.POST`）中与 [`initial`](#django.forms.Form.initial) 中提供的数据不同的字段名列表。如果没有不同的数据，则返回一个空列表。

```
>>> f = ContactForm(request.POST, initial=data)
>>> if f.has_changed():
...     print("The following fields changed: %s" % ", ".join(f.changed_data))
>>> f.changed_data
['subject', 'message']
```

## 访问表单中的字段

#### `Form.fields`

您可以通过表单实例的 `fields` 属性访问表单的字段：

```pycon
>>> for row in f.fields.values():
...     print(row)
...
<django.forms.fields.CharField object at 0x7ffaac632510>
<django.forms.fields.URLField object at 0x7ffaac632f90>
<django.forms.fields.CharField object at 0x7ffaac3aa050>
>>> f.fields["name"]
<django.forms.fields.CharField object at 0x7ffaac6324d0>
```

您可以更改表单实例的字段和 [`BoundField`](#django.forms.BoundField)，以改变它在表单中的呈现方式：

```pycon
>>> f.as_div().split("</div>")[0]
'<div><label for="id_subject">Subject:</label><input type="text" name="subject" maxlength="100" required id="id_subject">'
>>> f["subject"].label = "Topic"
>>> f.as_div().split("</div>")[0]
'<div><label for="id_subject">Topic:</label><input type="text" name="subject" maxlength="100" required id="id_subject">'
```

小心不要修改 `base_fields` 属性，因为这种修改会影响到同一个 Python 进程中所有后续的 `ContactForm` 实例：

```pycon
>>> f.base_fields["subject"].label_suffix = "?"
>>> another_f = CommentForm(auto_id=False)
>>> f.as_div().split("</div>")[0]
'<div><label for="id_subject">Subject?</label><input type="text" name="subject" maxlength="100" required id="id_subject">'
```

## 访问“干净的”数据

#### `Form.cleaned_data`

[`Form`](#django.forms.Form) 类中的每个字段不仅负责验证数据，还负责“清理”数据——将其规范为一致的格式。这是一个很好的功能，因为它允许以各种方式输入特定字段的数据，并总是产生一致的输出。

例如， [`DateField`](/zh-hans/4.2/ref/forms/fields/#django.forms.DateField) 将输入规范化为 Python 的 `datetime.date` 对象。无论你传递给它的是格式为 `'1994-07-15'` 的字符串，还是 `datetime.date` 对象，或者其他一些格式，只要它是有效的，`DateField` 都会将它规范化为 `datetime.date` 对象。

一旦你创建了一个带有一组数据并进行了验证的 [`Form`](#django.forms.Form) 实例，你可以通过它的 `cleaned_data` 属性访问清洁后的数据：

```pycon
>>> data = {
...     "subject": "hello",
...     "message": "Hi there",
...     "sender": "foo@example.com",
...     "cc_myself": True,
... }
>>> f = ContactForm(data)
>>> f.is_valid()
True
>>> f.cleaned_data
{'cc_myself': True, 'message': 'Hi there', 'sender': 'foo@example.com', 'subject': 'hello'}
```

请注意，任何基于文本的字段——如 `CharField` 或 `EmailField`——总是将输入清理成一个字符串。我们将在本文档后面介绍编码的含义。

如果你的数据验证不通过，`cleaned_data` 字典仅包含有效的字段：

```pycon
>>> data = {
...     "subject": "",
...     "message": "Hi there",
...     "sender": "invalid email address",
...     "cc_myself": True,
... }
>>> f = ContactForm(data)
>>> f.is_valid()
False
>>> f.cleaned_data
{'cc_myself': True, 'message': 'Hi there'}
```

`cleaned_data` 将始终仅包含在 `Form` 中定义的字段的键，即使在定义 `Form` 时传递了额外的数据。在这个例子中，我们在 `ContactForm` 构造函数中传递了许多额外字段，但 `cleaned_data` 仅包含表单的字段：

```pycon
>>> data = {
...     "subject": "hello",
...     "message": "Hi there",
...     "sender": "foo@example.com",
...     "cc_myself": True,
...     "extra_field_1": "foo",
...     "extra_field_2": "bar",
...     "extra_field_3": "baz",
... }
>>> f = ContactForm(data)
>>> f.is_valid()
True
>>> f.cleaned_data  # Doesn't contain extra_field_1, etc.
{'cc_myself': True, 'message': 'Hi there', 'sender': 'foo@example.com', 'subject': 'hello'}
```

当 `Form` 有效时，即使数据中没有为一些可选字段提供值，`cleaned_data` 也会包括所有字段的键和值。在这个例子中，数据字典没有包含 `nick_name` 字段的值，但 `cleaned_data` 包括它，并带有一个空值：

```pycon
>>> from django import forms
>>> class OptionalPersonForm(forms.Form):
...     first_name = forms.CharField()
...     last_name = forms.CharField()
...     nick_name = forms.CharField(required=False)
...
>>> data = {"first_name": "John", "last_name": "Lennon"}
>>> f = OptionalPersonForm(data)
>>> f.is_valid()
True
>>> f.cleaned_data
{'nick_name': '', 'first_name': 'John', 'last_name': 'Lennon'}
```

在上面这个例子中，`nick_name` 的 `cleaned_data` 值被设置为一个空字符串，因为 `nick_name` 是 `CharField`，而 `CharField` 将空值视为空字符串。每个字段类型都知道它的“空”值是什么——例如，对于 `DateField`，它是 `None` 而不是空字符串。关于每个字段在这种情况下的行为的全部细节，请参阅下面“内置 `Field` 类”一节中每个字段的“空值”说明。

你可以编写代码来对特定的表单字段（基于其名称）或整个表单（考虑各种字段的组合）进行验证。更多关于这方面的信息请参见 [表单和字段验证](/zh-hans/4.2/ref/forms/validation/)。

## 将表单输出为 HTML

一个 `Form` 对象的第二个任务是将自己呈现为 HTML。要做到这一点，只需将它打印出来：

```pycon
>>> f = ContactForm()
>>> print(f)
<tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" required></td></tr>
<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" required></td></tr>
<tr><th><label for="id_sender">Sender:</label></th><td><input type="email" name="sender" id="id_sender" required></td></tr>
<tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself"></td></tr>
```

如果表单与数据绑定，HTML 输出将适当地包含该数据。例如，如果一个字段由 `<input type="text">` 表示，数据将位于 `value` 属性中。如果一个字段由 `<input type="checkbox">` 表示，那么 HTML 中将包含 checked\`\`（如果适用）：

```pycon
>>> data = {
...     "subject": "hello",
...     "message": "Hi there",
...     "sender": "foo@example.com",
...     "cc_myself": True,
... }
>>> f = ContactForm(data)
>>> print(f)
<tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" value="hello" required></td></tr>
<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" value="Hi there" required></td></tr>
<tr><th><label for="id_sender">Sender:</label></th><td><input type="email" name="sender" id="id_sender" value="foo@example.com" required></td></tr>
<tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself" checked></td></tr>
```

这个默认输出是一个两列的 HTML 表单，每个字段都有一个 `<tr>`。请注意以下几点：

- 为灵活起见，输出 *不* 包括 `<table>` 和 `</table>` 标签，也不包括 `<form>` 和 `</form>` 标签或 `<input type="submit">` 标签。这是你的工作。
- 每个字段类型都有一个默认的 HTML 表示。`CharField` 用 `<input type="text">` 表示，`EmailField` 用 `<input type="email">` 表示。`BooleanField(null=False)` 由一个 `<input type="checkbox">`。请注意，这些只是合理的默认值；你可以通过使用部件来指定一个给定字段使用的 HTML，我们将在后面解释。
- 每个标签的 HTML `name` 直接从 `ContactForm` 类中的属性名中提取。
- 每个字段的文本标签——例如 `'Subject:'`、`'Message:'` 和 `'Cc myself:'`，是根据字段名将所有下划线转换为空格并将第一个字母大写而产生的。同样，请注意这些只是合理的默认值；你也可以手动指定标签。
- 每个文本标签都被一个 HTML `<label>` 标签包围，该标签通过其 `id` 指向相应的表格字段。而它的 `id` 则是通过在字段名前加上 `'id_'` 生成的。`id` 属性和 `<label>` 标签默认包含在输出中，以遵循最佳实践，但你可以改变这种行为。
- 输出使用 HTML5 语法，目标是 `<!DOCTYPE html>`。例如，它使用布尔属性，如 `checked` 而不是 XHTML 风格的 `checked='checked'`。

虽然当你 `print` 表单时，`<table>` 输出是默认的输出样式，但还有其他的输出样式。每种样式都可以作为表单对象的一个方法，每个渲染方法都返回一个字符串。

### 默认渲染

当你打印一个表单时，默认的渲染使用以下方法和属性。

#### `template_name`

#### `Form.template_name`

如果表单被转换为字符串，例如通过 `print(form)` 或在模板中通过 `{{ form }}`，则渲染的模板名称。

默认情况下，它返回渲染器的 [`form_template_name`](/zh-hans/4.2/ref/forms/renderers/#django.forms.renderers.BaseRenderer.form_template_name) 的值。你可以将其设置为一个字符串模板名称，以便在特定的表单类中进行覆盖。

> **Changed in Django 4.1**
>
> 在旧版本中，`template_name` 默认为字符串值 `'django/forms/default.html'`。

#### `render()`

#### `Form.render(template_name=None, context=None, renderer=None)`

渲染方法被 `__str__` 以及 [`Form.as_table()`](#django.forms.Form.as_table)、[`Form.as_p()`](#django.forms.Form.as_p) 和 [`Form.as_ul()`](#django.forms.Form.as_ul) 方法调用。所有的参数都是可选的，默认为：

- `template_name`： [`Form.template_name`](#django.forms.Form.template_name)
- `context`：由 [`Form.get_context()`](#django.forms.Form.get_context) 返回的值
- `renderer`： 由 [`Form.default_renderer`](#django.forms.Form.default_renderer) 返回的值

通过传递 `template_name`，你可以自定义仅用于单个调用的模板。

#### `get_context()`

#### `Form.get_context()`

返回用于渲染表单的模板上下文。

可用的上下文：

- `form`: 绑定表单
- `fields`： 所有绑定字段，除了隐藏字段。
- ·hidden\_fields\`： 所有隐藏的绑定字段。
- `errors`： 所有与字段无关的或与隐藏字段有关的表单错误。

#### `template_name_label`

#### `Form.template_name_label`

用于渲染字段的 `<label>` 的模板，当调用 [`BoundField.label_tag()`](#django.forms.BoundField.label_tag)/[`legend_tag()`](#django.forms.BoundField.legend_tag) 时使用。可以通过覆盖此属性来更改每个表单的模板，也可以通过覆盖默认模板来更一般地更改，参见 [覆盖内置表单模板](/zh-hans/4.2/ref/forms/renderers/#overriding-built-in-form-templates)。

### 输出样式

除了直接渲染表单，例如在模板中使用 `{{ form }}`，以下辅助函数也作为代理到 [`Form.render()`](#django.forms.Form.render)，传递特定的 `template_name` 值。

这些辅助函数在模板中非常有用，当你需要覆盖表单渲染器或表单提供的值，但不能传递额外的参数给 [`render()`](#django.forms.Form.render) 时。例如，你可以使用 `{{ form.as_ul }}` 将一个表单渲染为无序列表。

每个辅助函数将一个表单方法与一个给出适当模板名称的属性配对。

#### `as_div()`

#### `Form.template_name_div`

> **New in Django 4.1**

`as_div()` 使用的模板。默认值：`'django/forms/div.html'`。

#### `Form.as_div()`

> **New in Django 4.1**

`as_div()` 将表单呈现为一系列 `<div>` 元素，每个 `<div>` 包含一个字段，例如：

```pycon
>>> f = ContactForm()
>>> f.as_div()
```

...生成的 HTML 如下：

```html
<div>
<label for="id_subject">Subject:</label>
<input type="text" name="subject" maxlength="100" required id="id_subject">
</div>
<div>
<label for="id_message">Message:</label>
<input type="text" name="message" required id="id_message">
</div>
<div>
<label for="id_sender">Sender:</label>
<input type="email" name="sender" required id="id_sender">
</div>
<div>
<label for="id_cc_myself">Cc myself:</label>
<input type="checkbox" name="cc_myself" id="id_cc_myself">
</div>
```

> **Note**
>
> 在框架提供的模板和输出样式中，建议使用 `as_div()` 而不是 `as_p()`、`as_table()` 和 `as_ul()` 版本，因为该模板实现了 `<fieldset>` 和 `<legend>` 来分组相关的输入，并且对于屏幕阅读器用户更容易导航。

#### `as_p()`

#### `Form.template_name_p`

由 `as_p()` 使用的模板。默认值为 `'django/forms/p.html'`。

#### `Form.as_p()`

`as_p()` 将表单呈现为一系列 `<p>` 标签，每个 `<p>` 包含一个字段：

```pycon
>>> f = ContactForm()
>>> f.as_p()
'<p><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" required></p>\n<p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" required></p>\n<p><label for="id_sender">Sender:</label> <input type="text" name="sender" id="id_sender" required></p>\n<p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself"></p>'
>>> print(f.as_p())
<p><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" required></p>
<p><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" required></p>
<p><label for="id_sender">Sender:</label> <input type="email" name="sender" id="id_sender" required></p>
<p><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself"></p>
```

#### `as_ul()`

#### `Form.template_name_ul`

由 `as_ul()` 使用的模板。默认值为 `'django/forms/ul.html'`。

#### `Form.as_ul()`

`as_ul()` 将表单呈现为一系列 `<li>` 标签，每个 `<li>` 包含一个字段。它不包括 `<ul>` 或 `</ul>`，这样你可以在 `<ul>` 上指定任何HTML属性以增加灵活性：

```pycon
>>> f = ContactForm()
>>> f.as_ul()
'<li><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" required></li>\n<li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" required></li>\n<li><label for="id_sender">Sender:</label> <input type="email" name="sender" id="id_sender" required></li>\n<li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself"></li>'
>>> print(f.as_ul())
<li><label for="id_subject">Subject:</label> <input id="id_subject" type="text" name="subject" maxlength="100" required></li>
<li><label for="id_message">Message:</label> <input type="text" name="message" id="id_message" required></li>
<li><label for="id_sender">Sender:</label> <input type="email" name="sender" id="id_sender" required></li>
<li><label for="id_cc_myself">Cc myself:</label> <input type="checkbox" name="cc_myself" id="id_cc_myself"></li>
```

#### `as_table()`

#### `Form.template_name_table`

由 `as_table()` 使用的模板。默认值为 `'django/forms/table.html'`。

#### `Form.as_table()`

`as_table()` 将表单呈现为 HTML 的 `<table>`：

```pycon
>>> f = ContactForm()
>>> f.as_table()
'<tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" required></td></tr>\n<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" required></td></tr>\n<tr><th><label for="id_sender">Sender:</label></th><td><input type="email" name="sender" id="id_sender" required></td></tr>\n<tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself"></td></tr>'
>>> print(f)
<tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" type="text" name="subject" maxlength="100" required></td></tr>
<tr><th><label for="id_message">Message:</label></th><td><input type="text" name="message" id="id_message" required></td></tr>
<tr><th><label for="id_sender">Sender:</label></th><td><input type="email" name="sender" id="id_sender" required></td></tr>
<tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input type="checkbox" name="cc_myself" id="id_cc_myself"></td></tr>
```

### 样式化必填或错误的表单行

#### `Form.error_css_class`

#### `Form.required_css_class`

对必填或有错误的表单行和字段进行样式设计是很常见的。例如，你可能想用粗体显示必填的表格行，用红色突出显示错误。

[`Form`](#django.forms.Form) 类有几个钩子，你可以用来给必填行或有错误的行添加 `class` 属性：设置 [`Form.error_css_class`](#django.forms.Form.error_css_class) 和／或 [`Form.required_css_class`](#django.forms.Form.required_css_class) 属性：

```
from django import forms

class ContactForm(forms.Form):
    error_css_class = "error"
    required_css_class = "required"

    # ... and the rest of your fields here
```

一旦你这样做了，行将根据需要被赋予 `"error"` 和/或 `"required"` 类。HTML 将类似于：

```pycon
>>> f = ContactForm(data)
>>> print(f.as_table())
<tr class="required"><th><label class="required" for="id_subject">Subject:</label>    ...
<tr class="required"><th><label class="required" for="id_message">Message:</label>    ...
<tr class="required error"><th><label class="required" for="id_sender">Sender:</label>      ...
<tr><th><label for="id_cc_myself">Cc myself:<label> ...
>>> f["subject"].label_tag()
<label class="required" for="id_subject">Subject:</label>
>>> f["subject"].legend_tag()
<legend class="required" for="id_subject">Subject:</legend>
>>> f["subject"].label_tag(attrs={"class": "foo"})
<label for="id_subject" class="foo required">Subject:</label>
>>> f["subject"].legend_tag(attrs={"class": "foo"})
<legend for="id_subject" class="foo required">Subject:</legend>
```

### 设置表单元素的 HTML `id` 属性和 `<label>` 标签。

#### `Form.auto_id`

默认情况下，表单渲染方法包括：

- 表单元素的 HTML `id` 属性。
- 标签周围对应的 `<label>` 标签。HTML `<label>` 标签指定了哪个标签文本与哪个表单元素相关联。这个小小的改进使表单更加可用，也更容易被辅助设备访问。使用 `<label>` 标签总是一个好主意。

`id` 属性值是通过将 `id_` 预置到表单字段名后生成的。 如果你想改变 `id` 惯例或完全删除 HTML `id` 属性和 `<label>` 标签，这种行为是可以设置的。

使用 `Form` 构造函数的 `auto_id` 参数来控制 `id` 和标签行为。这个参数必须是 `True`、`False` 或一个字符串。

如果 `auto_id` 是 `False`，那么表单输出将不包含 `<label>` 标签和 `id` 属性：

```pycon
>>> f = ContactForm(auto_id=False)
>>> print(f.as_div())
<div>Subject:<input type="text" name="subject" maxlength="100" required></div>
<div>Message:<textarea name="message" cols="40" rows="10" required></textarea></div>
<div>Sender:<input type="email" name="sender" required></div>
<div>Cc myself:<input type="checkbox" name="cc_myself"></div>
```

如果 `auto_id` 设置为 `True`，那么表单输出将包含 `<label>` 标签，并且将使用字段名作为每个表单字段的 `id`：

```pycon
>>> f = ContactForm(auto_id=True)
>>> print(f.as_div())
<div><label for="subject">Subject:</label><input type="text" name="subject" maxlength="100" required id="subject"></div>
<div><label for="message">Message:</label><textarea name="message" cols="40" rows="10" required id="message"></textarea></div>
<div><label for="sender">Sender:</label><input type="email" name="sender" required id="sender"></div>
<div><label for="cc_myself">Cc myself:</label><input type="checkbox" name="cc_myself" id="cc_myself"></div>
```

如果 `auto_id` 设置为包含格式字符 `'%s'` 的字符串，那么表单输出将包含 `<label>` 标签，并且将根据格式字符串生成 `id` 属性。例如，对于格式字符串 `'field_%s'`，名为 `subject` 的字段将获得 `id` 值 `'field_subject'`。继续我们的例子：

```pycon
>>> f = ContactForm(auto_id="id_for_%s")
>>> print(f.as_div())
<div><label for="id_for_subject">Subject:</label><input type="text" name="subject" maxlength="100" required id="id_for_subject"></div>
<div><label for="id_for_message">Message:</label><textarea name="message" cols="40" rows="10" required id="id_for_message"></textarea></div>
<div><label for="id_for_sender">Sender:</label><input type="email" name="sender" required id="id_for_sender"></div>
<div><label for="id_for_cc_myself">Cc myself:</label><input type="checkbox" name="cc_myself" id="id_for_cc_myself"></div>
```

如果 `auto_id` 被设置为任何其他的真值——比如一个不包含 `%s` 的字符串——那么该库就会像 `auto_id` 是 `True` 一样。

默认情况下，`auto_id` 被设置为字符串 `'id_%s'`。

#### `Form.label_suffix`

一个可翻译的字符串（英文默认为冒号（`:`）），将在渲染表格时附加在任何标签名称之后。

可以使用 `label_suffix` 参数自定义该字符，或完全省略它：

```pycon
>>> f = ContactForm(auto_id="id_for_%s", label_suffix="")
>>> print(f.as_div())
<div><label for="id_for_subject">Subject</label><input type="text" name="subject" maxlength="100" required id="id_for_subject"></div>
<div><label for="id_for_message">Message</label><textarea name="message" cols="40" rows="10" required id="id_for_message"></textarea></div>
<div><label for="id_for_sender">Sender</label><input type="email" name="sender" required id="id_for_sender"></div>
<div><label for="id_for_cc_myself">Cc myself</label><input type="checkbox" name="cc_myself" id="id_for_cc_myself"></div>
>>> f = ContactForm(auto_id="id_for_%s", label_suffix=" ->")
>>> print(f.as_div())
<div><label for="id_for_subject">Subject:</label><input type="text" name="subject" maxlength="100" required id="id_for_subject"></div>
<div><label for="id_for_message">Message -&gt;</label><textarea name="message" cols="40" rows="10" required id="id_for_message"></textarea></div>
<div><label for="id_for_sender">Sender -&gt;</label><input type="email" name="sender" required id="id_for_sender"></div>
<div><label for="id_for_cc_myself">Cc myself -&gt;</label><input type="checkbox" name="cc_myself" id="id_for_cc_myself"></div>
```

请注意，只有当标签的最后一个字符不是标点符号时，才会加上标签后缀（在英文中，这些字符是 `.`、`!`、`?` 或 `:`）。

字段还可以定义自己的 [`label_suffix`](/zh-hans/4.2/ref/forms/fields/#django.forms.Field.label_suffix)。这将优先于 [`Form.label_suffix`](#django.forms.Form.label_suffix)。后缀也可以在运行时使用 `label_suffix` 参数来覆盖 [`label_tag()`](#django.forms.BoundField.label_tag)/ [`legend_tag()`](#django.forms.BoundField.legend_tag)。

#### `Form.use_required_attribute`

当设置为 `True` （默认）时，必填表单字段将有 `required` HTML 属性。

[表单集](/zh-hans/4.2/topics/forms/formsets/) 实例化表单时使用 `use_required_attribute=False` 以避免从表单集中添加和删除表单时浏览器验证错误。

### 设置表单组件的渲染方式

#### `Form.default_renderer`

指定 [渲染器](/zh-hans/4.2/ref/forms/renderers/) 用于表单的渲染。默认值为 `None`，表示使用 [`FORM_RENDERER`](/zh-hans/4.2/ref/settings/#std-setting-FORM_RENDERER) 设置中指定的默认渲染器。

你可以在声明你的表单时将其设置为一个类属性，或者使用 `Form.__init__()` 的 `renderer` 参数。例如：

```
from django import forms

class MyForm(forms.Form):
    default_renderer = MyRenderer()
```

或者：

```
form = MyForm(renderer=MyRenderer())
```

### 字段顺序的注意事项

在 `as_p()`、`as_ul()` 和 `as_table()` 快捷方式中，字段是按照你在表单类中定义的顺序显示的。例如，在 `ContactForm` 的例子中，字段是按照 `subject`、`message`、`sender`、`cc_myself` 的顺序定义的。要调整 HTML 输出的顺序，改变这些字段在类中的排列顺序。

还有其他几种方式可以自定义顺序：

#### `Form.field_order`

默认情况下 `Form.field_order=None`，它保留了你在表单类中定义字段的顺序。如果 `field_order` 是一个字段名的列表，则字段按列表指定的顺序排列，其余字段按默认顺序追加。列表中未知的字段名将被忽略。这使得在子类中可以通过将字段设置为 `None` 来禁用字段，而不必重新定义排序。

你也可以使用 [`Form`](#django.forms.Form) 的 `Form.field_order` 参数来覆盖字段顺序。如果一个 [`Form`](#django.forms.Form) 定义了 [`field_order`](#django.forms.Form.field_order)，*并且* 你在实例化 `Form` 时包含了 `field_order`，那么后者的 `field_order` 将具有优先权。

#### `Form.order_fields(field_order)`

你可以在任何时候使用 `order_fields()` 对字段进行重新排列，字段名列表如 [`field_order`](#django.forms.Form.field_order)。

### 如何显示错误

如果渲染一个已绑定的 `Form` 对象，渲染的操作会自动运行表单的验证，如果尚未发生验证，HTML 输出将包含验证错误作为一个接近字段的 `<ul class="errorlist">`。错误消息的具体位置取决于你使用的输出方法：

```pycon
>>> data = {
...     "subject": "",
...     "message": "Hi there",
...     "sender": "invalid email address",
...     "cc_myself": True,
... }
>>> f = ContactForm(data, auto_id=False)
>>> print(f.as_div())
<div>Subject:<ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="subject" maxlength="100" required></div>
<div>Message:<textarea name="message" cols="40" rows="10" required>Hi there</textarea></div>
<div>Sender:<ul class="errorlist"><li>Enter a valid email address.</li></ul><input type="email" name="sender" value="invalid email address" required></div>
<div>Cc myself:<input type="checkbox" name="cc_myself" checked></div>
>>> print(f.as_table())
<tr><th>Subject:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="subject" maxlength="100" required></td></tr>
<tr><th>Message:</th><td><textarea name="message" cols="40" rows="10" required></textarea></td></tr>
<tr><th>Sender:</th><td><ul class="errorlist"><li>Enter a valid email address.</li></ul><input type="email" name="sender" value="invalid email address" required></td></tr>
<tr><th>Cc myself:</th><td><input checked type="checkbox" name="cc_myself"></td></tr>
>>> print(f.as_ul())
<li><ul class="errorlist"><li>This field is required.</li></ul>Subject: <input type="text" name="subject" maxlength="100" required></li>
<li>Message: <textarea name="message" cols="40" rows="10" required></textarea></li>
<li><ul class="errorlist"><li>Enter a valid email address.</li></ul>Sender: <input type="email" name="sender" value="invalid email address" required></li>
<li>Cc myself: <input checked type="checkbox" name="cc_myself"></li>
>>> print(f.as_p())
<p><ul class="errorlist"><li>This field is required.</li></ul></p>
<p>Subject: <input type="text" name="subject" maxlength="100" required></p>
<p>Message: <textarea name="message" cols="40" rows="10" required></textarea></p>
<p><ul class="errorlist"><li>Enter a valid email address.</li></ul></p>
<p>Sender: <input type="email" name="sender" value="invalid email address" required></p>
<p>Cc myself: <input checked type="checkbox" name="cc_myself"></p>
```

### 自定义错误列表格式

#### `class ErrorList(initlist=None, error_class=None, renderer=None)`

默认情况下，表单使用 `django.forms.utils.ErrorList` 来格式化验证错误。`ErrorList` 是一个类似列表的对象，其中 `initlist` 是错误列表。此外，这个类有以下属性和方法。

#### `error_class`

渲染错误列表时要使用的 CSS 类。任何提供的类将被添加到默认的 `errorlist` 类中。

#### `renderer`

指定 [渲染器](/zh-hans/4.2/ref/forms/renderers/) 用于 `ErrorList`。默认为 `None`，即使用由 `FORM_RENDER` 配置指定的默认渲染器。

#### `template_name`

调用 `__str__` 或 [`render()`](#django.forms.ErrorList.render) 时使用的模板名称。默认情况下，这是 `'django/forms/errors/list/default.html'`，它是 `'ul.html'` 模板的代理。

#### `template_name_text`

调用 [`as_text()`](#django.forms.ErrorList.as_text) 时使用的模板名称。默认是 `'django/forms/errors/list/text.html'`。该模板将错误显示为一个列表，其中包含了一些要点。

#### `template_name_ul`

调用 [`as_ul()`](#django.forms.ErrorList.as_ul) 时使用的模板名称。默认情况下，这是 `'django/forms/errors/list/ul.html'`。这个模板在 `<li>` 标签中渲染错误，并使用 [`error_class`](#django.forms.ErrorList.error_class) 所定义的 CSS 类来包装 `<ul>`。

#### `get_context()`

返回模板中渲染错误的上下文。

可用的上下文：

- `errors`： 一个错误列表。
- `error_class`：一个 CSS 类的字符串。

#### `render(template_name=None, context=None, renderer=None)`

渲染方法被 `__str__` 以及 [`as_ul()`](#django.forms.ErrorList.as_ul) 方法所调用。

所有参数都是可选的，将默认为：

- `template_name`：由 [`template_name`](#django.forms.ErrorList.template_name) 返回的值。
- `context`： 由 [`get_context()`](#django.forms.ErrorList.get_context) 返回的值
- `renderer`： 由 [`renderer`](#django.forms.ErrorList.renderer) 返回的值

#### `as_text()`

使用由 [`template_name_text`](#django.forms.ErrorList.template_name_text) 定义的模板渲染错误列表。

#### `as_ul()`

使用由 [`template_name_ul`](#django.forms.ErrorList.template_name_ul) 定义的模板渲染错误列表。

如果你想自定义错误的渲染，这可以通过覆盖 [`template_name`](#django.forms.ErrorList.template_name) 属性来实现，或者更普遍地通过覆盖默认模板来实现，也可以参见 [覆盖内置表单模板](/zh-hans/4.2/ref/forms/renderers/#overriding-built-in-form-templates)。

> **Deprecated since Django 4.0**
>
> Deprecated since version 4.0: 当调用 `__str__` 方法时，返回 `str` 的能力已被弃用。使用模板引擎代替，它返回一个 `SafeString`。

## 更精细的输出

`as_p()`、`as_ul()` 和 `as_table()` 方法都是快捷方式——它们并不是显示表单对象的唯一方式。

#### `class BoundField`

用于显示 [`Form`](#django.forms.Form) 实例的单个字段的 HTML 或访问属性。

该对象的 `__str__()` 方法显示该字段的 HTML。

要检索单个 `BoundField`，可以在表单上使用字段的名称作为键进行字典查找：

```pycon
>>> form = ContactForm()
>>> print(form["subject"])
<input id="id_subject" type="text" name="subject" maxlength="100" required>
```

要检索所有的 `BoundField` 对象，可以迭代表单：

```pycon
>>> form = ContactForm()
>>> for boundfield in form:
...     print(boundfield)
...
<input id="id_subject" type="text" name="subject" maxlength="100" required>
<input type="text" name="message" id="id_message" required>
<input type="email" name="sender" id="id_sender" required>
<input type="checkbox" name="cc_myself" id="id_cc_myself">
```

字段特定的输出遵循表单对象的 `auto_id` 设置：

```pycon
>>> f = ContactForm(auto_id=False)
>>> print(f["message"])
<input type="text" name="message" required>
>>> f = ContactForm(auto_id="id_%s")
>>> print(f["message"])
<input type="text" name="message" id="id_message" required>
```

### `BoundField` 的属性

#### `BoundField.auto_id`

`BoundField` 的 HTML ID 属性。如果 [`Form.auto_id`](#django.forms.Form.auto_id) 是 `False`，则返回一个空字符串。

#### `BoundField.data`

该属性返回由 widget 的 [`value_from_datadict()`](/zh-hans/4.2/ref/forms/widgets/#django.forms.Widget.value_from_datadict) 方法提取的这个 [`BoundField`](#django.forms.BoundField) 的数据，如果没有提供数据则返回 `None`：

```pycon
>>> unbound_form = ContactForm()
>>> print(unbound_form["subject"].data)
None
>>> bound_form = ContactForm(data={"subject": "My Subject"})
>>> print(bound_form["subject"].data)
My Subject
```

#### `BoundField.errors`

一个类似列表的对象，在打印时显示为 HTML `<ul class="errorlist">`：

```pycon
>>> data = {"subject": "hi", "message": "", "sender": "", "cc_myself": ""}
>>> f = ContactForm(data, auto_id=False)
>>> print(f["message"])
<input type="text" name="message" required>
>>> f["message"].errors
['This field is required.']
>>> print(f["message"].errors)
<ul class="errorlist"><li>This field is required.</li></ul>
>>> f["subject"].errors
[]
>>> print(f["subject"].errors)

>>> str(f["subject"].errors)
''
```

#### `BoundField.field`

这个 [`Field`](/zh-hans/4.2/ref/forms/fields/#django.forms.Field) 封装的表单类中的表单 [`BoundField`](#django.forms.BoundField) 实例。

#### `BoundField.form`

这个 [`Form`](#django.forms.Form) 实例与这个 [`BoundField`](#django.forms.BoundField) 绑定。

#### `BoundField.help_text`

字段的 [`help_text`](/zh-hans/4.2/ref/forms/fields/#django.forms.Field.help_text)。

#### `BoundField.html_name`

部件的 HTML `name` 属性中使用的名称。它考虑到了 [`prefix`](#django.forms.Form.prefix) 的形式。

#### `BoundField.id_for_label`

使用这个属性来渲染该字段的 ID。例如，如果你在模板中手动构造一个 `<label>` （尽管 [`label_tag()`](#django.forms.BoundField.label_tag)/[`legend_tag()`](#django.forms.BoundField.legend_tag) 会为你执行此操作）：

```html+django
<label for="{{ form.my_field.id_for_label }}">...</label>{{ my_field }}
```

默认情况下，这将是字段的名称，前缀为 `id_` （上面的例子为 "`id_my_field`"）。你可以通过设置 [`attrs`](/zh-hans/4.2/ref/forms/widgets/#django.forms.Widget.attrs) 对字段的部件进行修改。例如，声明一个字段是这样的：

```
my_field = forms.CharField(widget=forms.TextInput(attrs={"id": "myFIELD"}))
```

并使用上面的模板，会呈现出这样的效果：

```html
<label for="myFIELD">...</label><input id="myFIELD" type="text" name="my_field" required>
```

#### `BoundField.initial`

使用 [`BoundField.initial`](#django.forms.BoundField.initial) 来检索一个表单字段的初始数据。如果存在，它从 [`Form.initial`](#django.forms.Form.initial) 检索数据，否则尝试 [`Field.initial`](/zh-hans/4.2/ref/forms/fields/#django.forms.Field.initial)。值为可调用对象将被执行。参见 [初始表单值](#ref-forms-initial-form-values) 获取更多的例子。

[`BoundField.initial`](#django.forms.BoundField.initial) 缓存其返回值，这在处理可调用对象的情况下特别有用，其返回值可能会改变（例如，`datetime.now` 或 `uuid.uuid4`）：

```pycon
>>> from datetime import datetime
>>> class DatedCommentForm(CommentForm):
...     created = forms.DateTimeField(initial=datetime.now)
...
>>> f = DatedCommentForm()
>>> f["created"].initial
datetime.datetime(2021, 7, 27, 9, 5, 54)
>>> f["created"].initial
datetime.datetime(2021, 7, 27, 9, 5, 54)
```

建议使用 [`BoundField.initial`](#django.forms.BoundField.initial) 而不是 [`get_initial_for_field()`](#django.forms.Form.get_initial_for_field)。

#### `BoundField.is_hidden`

如果这个 [`BoundField`](#django.forms.BoundField) 的部件被隐藏，返回 `True`。

#### `BoundField.label`

字段的 [`label`](/zh-hans/4.2/ref/forms/fields/#django.forms.Field.label)。这在 [`label_tag()`](#django.forms.BoundField.label_tag)/[`legend_tag()`](#django.forms.BoundField.legend_tag) 中使用。

#### `BoundField.name`

该字段在表单中的名称：

```pycon
>>> f = ContactForm()
>>> print(f["subject"].name)
subject
>>> print(f["message"].name)
message
```

#### `BoundField.use_fieldset`

> **New in Django 4.1**

返回该 BoundField widget 的 `use_fieldset` 属性的值。

#### `BoundField.widget_type`

返回封装字段 widget 的类名的小写形式，去除任何尾随的 `input` 或 `widget`。这可以在构建依赖于 widget 类型的表单时使用。例如：

```html+django
{% for field in form %}
    {% if field.widget_type == 'checkbox' %}
        # render one way
    {% else %}
        # render another way
    {% endif %}
{% endfor %}
```

### `BoundField` 方法

#### `BoundField.as_hidden(attrs=None, **kwargs)`

返回将其表示为 `<input type="hidden">` 的 HTML 字符串。

`**kwargs` 传递给 [`as_widget()`](#django.forms.BoundField.as_widget)。

这个方法主要在内部使用。你应该使用部件来代替。

#### `BoundField.as_widget(widget=None, attrs=None, only_initial=False)`

通过渲染通过的部件来渲染该字段，并添加作为 `attrs` 传递的任何 HTML 属性。 如果没有指定部件，那么将使用该字段的默认部件。

`only_initial` 是 Django 内部使用的，不应该明确设置。

#### `BoundField.css_classes(extra_classes=None)`

当你使用 Django 的渲染快捷方式时，CSS 类用于指示必填表单字段或包含错误的字段。如果你手动渲染一个表单，你可以使用 `css_classes` 方法访问这些 CSS 类：

```pycon
>>> f = ContactForm(data={"message": ""})
>>> f["message"].css_classes()
'required'
```

如果你想要提供一些额外的类，除了可能需要的错误和必填类，你可以将这些类作为参数提供：

```pycon
>>> f = ContactForm(data={"message": ""})
>>> f["message"].css_classes("foo bar")
'foo bar required'
```

#### `BoundField.label_tag(contents=None, attrs=None, label_suffix=None, tag=None)`

使用 [`Form.template_name_label`](#django.forms.Form.template_name_label) 指定的模板，为表单字段渲染一个标签。

可用的上下文：

- `field`：这个 [`BoundField`](#django.forms.BoundField) 的实例。
- `contents`：默认是 [`BoundField.label`](#django.forms.BoundField.label) 和 [`Form.label_suffix`](#django.forms.Form.label_suffix) （或者 [`Field.label_suffix`](/zh-hans/4.2/ref/forms/fields/#django.forms.Field.label_suffix), 如果设置的话）的连接字符串。这可以被 `contents` 和 `label_suffix` 参数所覆盖。
- `attrs`：一个包含 `for`、[`Form.required_css_class`](#django.forms.Form.required_css_class) 和 `id` 的 `dict`。`id` 是由字段的部件 `attrs` 或 [`BoundField.auto_id`](#django.forms.BoundField.auto_id) 产生的。其他的属性可以由 `attrs` 参数提供。
- `use_tag`: 一个布尔值，如果标签具有 `id`，则为 `True`。如果为 `False`，默认模板将省略 `tag`。
- `tag`: 可选的字符串，用于自定义标签，默认为 `label`。

> **Tip**
>
> 在你的模板中 `field` 是 `BoundField` 的实例。因此 `field.field` 访问 [`BoundField.field`](#django.forms.BoundField.field) 是你声明的字段，例如 `forms.CharField`。

要单独渲染表单字段的标签标签，可以调用其 `label_tag()` 方法：

```pycon
>>> f = ContactForm(data={"message": ""})
>>> print(f["message"].label_tag())
<label for="id_message">Message:</label>
```

如果你想自定义渲染，这可以通过覆盖 [`Form.template_name_label`](#django.forms.Form.template_name_label) 属性来实现，或者更普遍地通过覆盖默认模板来实现，也可以参见 [覆盖内置表单模板](/zh-hans/4.2/ref/forms/renderers/#overriding-built-in-form-templates)。

> **Changed in Django 4.1**
>
> 添加了 `tag` 参数。

#### `BoundField.legend_tag(contents=None, attrs=None, label_suffix=None)`

> **New in Django 4.1**

使用 `tag='legend'` 调用 [`label_tag()`](#django.forms.BoundField.label_tag) 以使用 `<legend>` 标签呈现标签。这在呈现单选按钮和多选框小部件时非常有用，其中 `<legend>` 可能比 `<label>` 更合适。

#### `BoundField.value()`

使用这个方法来呈现该字段的原始值，就像它由一个 `Widget` 呈现一样：

```pycon
>>> initial = {"subject": "welcome"}
>>> unbound_form = ContactForm(initial=initial)
>>> bound_form = ContactForm(data={"subject": "hi"}, initial=initial)
>>> print(unbound_form["subject"].value())
welcome
>>> print(bound_form["subject"].value())
hi
```

## 自定义 `BoundField`

如果你需要访问模板中表单字段的一些附加信息，而使用 [`Field`](/zh-hans/4.2/ref/forms/fields/#django.forms.Field) 的子类还不够，也可以考虑自定义 [`BoundField`](#django.forms.BoundField)。

自定义表单字段可以覆盖 `get_bound_field()`。

#### `Field.get_bound_field(form, field_name)`

取一个 [`Form`](#django.forms.Form) 的实例和字段名。当在模板中访问该字段时，将使用返回值。它很可能是 [`BoundField`](#django.forms.BoundField) 的一个子类的实例。

例如，如果你有一个 `GPSCoordinatesField`，并希望能够在模板中访问关于坐标的附加信息，可以按以下方式实现：

```
class GPSCoordinatesBoundField(BoundField):
    @property
    def country(self):
        """
        Return the country the coordinates lie in or None if it can't be
        determined.
        """
        value = self.value()
        if value:
            return get_country_from_coordinates(value)
        else:
            return None

class GPSCoordinatesField(Field):
    def get_bound_field(self, form, field_name):
        return GPSCoordinatesBoundField(form, self, field_name)
```

现在你可以在模板中使用 `{{form.coordinates.country }}` 访问国家。

## 将上传的文件绑定到表单中

处理有 `FileField` 和 `ImageField` 字段的表单比普通表单要复杂一些。

首先，为了上传文件，你需要确保你的 `<form>` 元素正确地将 `enctype` 定义为 `"multipart/form-data"`：

```html
<form enctype="multipart/form-data" method="post" action="/foo/">
```

其次，当你使用表单时，你需要绑定文件数据。文件数据与普通表单数据分开处理，所以当你的表单包含一个 `FileField` 和 `ImageField` 时，你需要在绑定表单时指定第二个参数。因此，如果我们扩展我们的 ContactForm 以包括一个名为 `mugshot` 的 `ImageField`，我们需要绑定包含头像图片的文件数据：

```pycon
# Bound form with an image field
>>> from django.core.files.uploadedfile import SimpleUploadedFile
>>> data = {
...     "subject": "hello",
...     "message": "Hi there",
...     "sender": "foo@example.com",
...     "cc_myself": True,
... }
>>> file_data = {"mugshot": SimpleUploadedFile("face.jpg", b"file data")}
>>> f = ContactFormWithMugshot(data, file_data)
```

在实际应用中，你通常会将 `request.FILES` 指定为文件数据的来源（就像你使用 `request.POST` 作为表单数据的来源一样）：

```pycon
# Bound form with an image field, data from the request
>>> f = ContactFormWithMugshot(request.POST, request.FILES)
```

构建一个未绑定的表单与以往一样，省略表单数据和文件数据：

```pycon
# Unbound form with an image field
>>> f = ContactFormWithMugshot()
```

### 多部分表格的测试

#### `Form.is_multipart()`

如果你正在编写可重用的视图或模板，你可能事先不知道你的表单是否是多部分表单。`is_multipart()` 方法告诉你是否需要多部分编码来提交表单：

```pycon
>>> f = ContactFormWithMugshot()
>>> f.is_multipart()
True
```

下面是一个在模板中使用的示例：

```html+django
{% if form.is_multipart %}
    <form enctype="multipart/form-data" method="post" action="/foo/">
{% else %}
    <form method="post" action="/foo/">
{% endif %}
{{ form }}
</form>
```

## 子类化表单

如果你有多个共享字段的 `Form` 类，你可以使用子类来消除冗余。

当你将一个自定义的 `Form` 类子类化时，生成的子类将包括父类的所有字段，然后是你在子类中定义的字段。

在这个例子中，`ContactFormWithPriority` 包含了来自 `ContactForm` 的所有字段，以及一个额外的字段 `priority`。`ContactForm` 的字段首先排列：

```pycon
>>> class ContactFormWithPriority(ContactForm):
...     priority = forms.CharField()
...
>>> f = ContactFormWithPriority(auto_id=False)
>>> print(f.as_div())
<div>Subject:<input type="text" name="subject" maxlength="100" required></div>
<div>Message:<textarea name="message" cols="40" rows="10" required></textarea></div>
<div>Sender:<input type="email" name="sender" required></div>
<div>Cc myself:<input type="checkbox" name="cc_myself"></div>
<div>Priority:<input type="text" name="priority" required></div>
```

可以子类化多个表单，将表单视为混合类。在这个例子中，`BeatleForm` 分别子类化了 `PersonForm` 和 `InstrumentForm` （按顺序），它的字段列表包括父类的字段：

```pycon
>>> from django import forms
>>> class PersonForm(forms.Form):
...     first_name = forms.CharField()
...     last_name = forms.CharField()
...
>>> class InstrumentForm(forms.Form):
...     instrument = forms.CharField()
...
>>> class BeatleForm(InstrumentForm, PersonForm):
...     haircut_type = forms.CharField()
...
>>> b = BeatleForm(auto_id=False)
>>> print(b.as_div())
<div>First name:<input type="text" name="first_name" required></div>
<div>Last name:<input type="text" name="last_name" required></div>
<div>Instrument:<input type="text" name="instrument" required></div>
<div>Haircut type:<input type="text" name="haircut_type" required></div>
```

可以通过在子类上将继承自父类的 `Field` 的名称设置为 `None` 来声明性地移除它。例如：

```pycon
>>> from django import forms

>>> class ParentForm(forms.Form):
...     name = forms.CharField()
...     age = forms.IntegerField()
...

>>> class ChildForm(ParentForm):
...     name = None
...

>>> list(ChildForm().fields)
['age']
```

## 表单前缀

#### `Form.prefix`

你可以将多个 Django 表单放在同一个 `<form>` 标签内。为每个 `Form` 提供单独的命名空间，可以使用 `prefix` 关键字参数：

```pycon
>>> mother = PersonForm(prefix="mother")
>>> father = PersonForm(prefix="father")
>>> print(mother.as_div())
<div><label for="id_mother-first_name">First name:</label><input type="text" name="mother-first_name" required id="id_mother-first_name"></div>
<div><label for="id_mother-last_name">Last name:</label><input type="text" name="mother-last_name" required id="id_mother-last_name"></div>
>>> print(father.as_div())
<div><label for="id_father-first_name">First name:</label><input type="text" name="father-first_name" required id="id_father-first_name"></div>
<div><label for="id_father-last_name">Last name:</label><input type="text" name="father-last_name" required id="id_father-last_name"></div>
```

命名空间也可以在表单类上指定：

```pycon
>>> class PersonForm(forms.Form):
...     ...
...     prefix = "person"
...
```
