# Django 5.2.8 documentation

# 使用基于类的视图处理表单

表单处理通常有3个途径：

- 初始 GET （空白或预填充表单）

- 带有合法数据的POST（通常是带有错误的重新显示的表单）

- 带有合法数据的POST（处理数据和通常重定向）

自己实现这个会导致很多重复的样板代码（查看 Using a form in a view ）。为了避免这个问题，Django 提供了一组通用基于类的视图来处理表单。

## 基础表单

提供联系表单：

`forms.py`

    from django import forms

    class ContactForm(forms.Form):
        name = forms.CharField()
        message = forms.CharField(widget=forms.Textarea)

        def send_email(self):
            # send email using the self.cleaned_data dictionary
            pass

可以使用 `FormView` 构建视图：

`views.py`

    from myapp.forms import ContactForm
    from django.views.generic.edit import FormView

    class ContactFormView(FormView):
        template_name = "contact.html"
        form_class = ContactForm
        success_url = "/thanks/"

        def form_valid(self, form):
            # This method is called when valid form data has been POSTed.
            # It should return an HttpResponse.
            form.send_email()
            return super().form_valid(form)

注意：

- FormView 继承了 `TemplateResponseMixin` ，因此可以在这里使用 `template_name` 。

- 默认实现了 `form_valid()` 简单重定向至 `success_url` 。

## 模型表单

Generic views really shine when working with models. These generic views will automatically create a `ModelForm`, so long as they can work out which model class to use:

- 如果已经给出了 `model` 属性，则使用这个模型类。

- If `get_object()` returns an object, the class of that object will be used.

- 如果已经给出了 `queryset` ，则使用这个查询集的模型。

Model form views provide a `form_valid()` implementation that saves the model automatically. You can override this if you have any special requirements; see below for examples.

You don't even need to provide a `success_url` for `CreateView` or `UpdateView` - they will use `get_absolute_url()` on the model object if available.

如果你想使用一个自定义的 `ModelForm` （比如添加额外的验证），用来在视图上设置 `form_class` 。

Note

当指定一个自定义表单类时，必须也要指定模型，即使 `form_class` 可能是一个 `ModelForm` 。

First we need to add `get_absolute_url()` to our `Author` class:

`models.py`

    from django.db import models
    from django.urls import reverse

    class Author(models.Model):
        name = models.CharField(max_length=200)

        def get_absolute_url(self):
            return reverse("author-detail", kwargs={"pk": self.pk})

然后可以使用 `CreateView` 并友好的执行实际工作。注意这里我们如何配置通用基于类的视图。我们不用自己编写任何逻辑：

`views.py`

    from django.urls import reverse_lazy
    from django.views.generic.edit import CreateView, DeleteView, UpdateView
    from myapp.models import Author

    class AuthorCreateView(CreateView):
        model = Author
        fields = ["name"]

    class AuthorUpdateView(UpdateView):
        model = Author
        fields = ["name"]

    class AuthorDeleteView(DeleteView):
        model = Author
        success_url = reverse_lazy("author-list")

Note

我们必须在这里使用 `reverse_lazy()` 来代替 `reverse()` ，因为在文件导入时不加载 urls 。

`fields` 属性的工作方式同 `ModelForm` 中内部 `Meta` 类的 `fields` 属性一样。除非你使用其他方式定义表单类，该属性是必需的，如果属性不存在，视图将引发 `ImproperlyConfigured` 异常。

如果同时指定了 `fields` 和 `form_class` 属性，将会引发 `ImproperlyConfigured` 异常。

最后将这些新视图挂钩到URLconf中：

`urls.py`

    from django.urls import path
    from myapp.views import AuthorCreateView, AuthorDeleteView, AuthorUpdateView

    urlpatterns = [
        # ...
        path("author/add/", AuthorCreateView.as_view(), name="author-add"),
        path("author/<int:pk>/", AuthorUpdateView.as_view(), name="author-update"),
        path("author/<int:pk>/delete/", AuthorDeleteView.as_view(), name="author-delete"),
    ]

Note

这些视图继承 `SingleObjectTemplateResponseMixin` ，它使用 `template_name_suffix` 来构建基于模型的 `template_name` 。

在这个例子里：

- `CreateView` 和 `UpdateView` 使用 `myapp/author_form.html` 。

- `DeleteView` 使用 `myapp/author_confirm_delete.html` 。

如果你想为 `CreateView` 和 `UpdateView` 制作单独的模板，你可以在视图类上设置 `template_name` 或 `template_name_suffix` 。

## 模型和 `request.user`

若要跟踪使用 `CreateView` 创建的用户，你可以使用自定义的 `ModelForm` 来执行此操作。首先，在模型里添加外键关系：

`models.py`

    from django.contrib.auth.models import User
    from django.db import models

    class Author(models.Model):
        name = models.CharField(max_length=200)
        created_by = models.ForeignKey(User, on_delete=models.CASCADE)

        # ...

In the view, ensure that you don't include `created_by` in the list of fields to edit, and override `form_valid()` to add the user:

`views.py`

    from django.contrib.auth.mixins import LoginRequiredMixin
    from django.views.generic.edit import CreateView
    from myapp.models import Author

    class AuthorCreateView(LoginRequiredMixin, CreateView):
        model = Author
        fields = ["name"]

        def form_valid(self, form):
            form.instance.created_by = self.request.user
            return super().form_valid(form)

`LoginRequiredMixin` prevents users who aren't logged in from accessing the form. If you omit that, you'll need to handle unauthorized users in `form_valid()`.

## 内容协商示例

下面是一个展示了如何实现基于API的工作流以及普通POST表单一起使用的表单：

    from django.http import JsonResponse
    from django.views.generic.edit import CreateView
    from myapp.models import Author

    class JsonableResponseMixin:
        """
        Mixin to add JSON support to a form.
        Must be used with an object-based FormView (e.g. CreateView)
        """

        def form_invalid(self, form):
            response = super().form_invalid(form)
            if self.request.accepts("text/html"):
                return response
            else:
                return JsonResponse(form.errors, status=400)

        def form_valid(self, form):
            # We make sure to call the parent's form_valid() method because
            # it might do some processing (in the case of CreateView, it will
            # call form.save() for example).
            response = super().form_valid(form)
            if self.request.accepts("text/html"):
                return response
            else:
                data = {
                    "pk": self.object.pk,
                }
                return JsonResponse(data)

    class AuthorCreateView(JsonableResponseMixin, CreateView):
        model = Author
        fields = ["name"]

The above example assumes that if the client supports `text/html`, that they would prefer it. However, this may not always be true. When requesting a `.css` file, many browsers will send the header `Accept: text/css,*/*;q=0.1`, indicating that they would prefer CSS, but anything else is fine. This means `request.accepts("text/html")` will be `True`.

To determine the correct format, taking into consideration the client's preference, use `django.http.HttpRequest.get_preferred_type()`:

    class JsonableResponseMixin:
        """
        Mixin to add JSON support to a form.
        Must be used with an object-based FormView (e.g. CreateView).
        """

        accepted_media_types = ["text/html", "application/json"]

        def dispatch(self, request, *args, **kwargs):
            if request.get_preferred_type(self.accepted_media_types) is None:
                # No format in common.
                return HttpResponse(
                    status_code=406, headers={"Accept": ",".join(self.accepted_media_types)}
                )

            return super().dispatch(request, *args, **kwargs)

        def form_invalid(self, form):
            response = super().form_invalid(form)
            accepted_type = self.request.get_preferred_type(self.accepted_media_types)
            if accepted_type == "text/html":
                return response
            elif accepted_type == "application/json":
                return JsonResponse(form.errors, status=400)

        def form_valid(self, form):
            # We make sure to call the parent's form_valid() method because
            # it might do some processing (in the case of CreateView, it will
            # call form.save() for example).
            response = super().form_valid(form)
            accepted_type = self.request.get_preferred_type(self.accepted_media_types)
            if accepted_type == "text/html":
                return response
            elif accepted_type == "application/json":
                data = {
                    "pk": self.object.pk,
                }
                return JsonResponse(data)

Changed in Django 5.2:

The `HttpRequest.get_preferred_type()` method was added.