generic-editing.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. ====================================
  2. Form handling with class-based views
  3. ====================================
  4. Form processing generally has 3 paths:
  5. * Initial GET (blank or prepopulated form)
  6. * POST with invalid data (typically redisplay form with errors)
  7. * POST with valid data (process the data and typically redirect)
  8. Implementing this yourself often results in a lot of repeated boilerplate code
  9. (see :ref:`Using a form in a view<using-a-form-in-a-view>`). To help avoid
  10. this, Django provides a collection of generic class-based views for form
  11. processing.
  12. Basic forms
  13. ===========
  14. Given a contact form:
  15. .. code-block:: python
  16. :caption: ``forms.py``
  17. from django import forms
  18. class ContactForm(forms.Form):
  19. name = forms.CharField()
  20. message = forms.CharField(widget=forms.Textarea)
  21. def send_email(self):
  22. # send email using the self.cleaned_data dictionary
  23. pass
  24. The view can be constructed using a ``FormView``:
  25. .. code-block:: python
  26. :caption: ``views.py``
  27. from myapp.forms import ContactForm
  28. from django.views.generic.edit import FormView
  29. class ContactFormView(FormView):
  30. template_name = "contact.html"
  31. form_class = ContactForm
  32. success_url = "/thanks/"
  33. def form_valid(self, form):
  34. # This method is called when valid form data has been POSTed.
  35. # It should return an HttpResponse.
  36. form.send_email()
  37. return super().form_valid(form)
  38. Notes:
  39. * FormView inherits
  40. :class:`~django.views.generic.base.TemplateResponseMixin` so
  41. :attr:`~django.views.generic.base.TemplateResponseMixin.template_name`
  42. can be used here.
  43. * The default implementation for
  44. :meth:`~django.views.generic.edit.FormMixin.form_valid` simply
  45. redirects to the :attr:`~django.views.generic.edit.FormMixin.success_url`.
  46. Model forms
  47. ===========
  48. Generic views really shine when working with models. These generic
  49. views will automatically create a :class:`~django.forms.ModelForm`, so long as
  50. they can work out which model class to use:
  51. * If the :attr:`~django.views.generic.edit.ModelFormMixin.model` attribute is
  52. given, that model class will be used.
  53. * If :meth:`~django.views.generic.detail.SingleObjectMixin.get_object()`
  54. returns an object, the class of that object will be used.
  55. * If a :attr:`~django.views.generic.detail.SingleObjectMixin.queryset` is
  56. given, the model for that queryset will be used.
  57. Model form views provide a
  58. :meth:`~django.views.generic.edit.ModelFormMixin.form_valid()` implementation
  59. that saves the model automatically. You can override this if you have any
  60. special requirements; see below for examples.
  61. You don't even need to provide a ``success_url`` for
  62. :class:`~django.views.generic.edit.CreateView` or
  63. :class:`~django.views.generic.edit.UpdateView` - they will use
  64. :meth:`~django.db.models.Model.get_absolute_url()` on the model object if available.
  65. If you want to use a custom :class:`~django.forms.ModelForm` (for instance to
  66. add extra validation), set
  67. :attr:`~django.views.generic.edit.FormMixin.form_class` on your view.
  68. .. note::
  69. When specifying a custom form class, you must still specify the model,
  70. even though the :attr:`~django.views.generic.edit.FormMixin.form_class` may
  71. be a :class:`~django.forms.ModelForm`.
  72. First we need to add :meth:`~django.db.models.Model.get_absolute_url()` to our
  73. ``Author`` class:
  74. .. code-block:: python
  75. :caption: ``models.py``
  76. from django.db import models
  77. from django.urls import reverse
  78. class Author(models.Model):
  79. name = models.CharField(max_length=200)
  80. def get_absolute_url(self):
  81. return reverse("author-detail", kwargs={"pk": self.pk})
  82. Then we can use :class:`CreateView` and friends to do the actual
  83. work. Notice how we're just configuring the generic class-based views
  84. here; we don't have to write any logic ourselves:
  85. .. code-block:: python
  86. :caption: ``views.py``
  87. from django.urls import reverse_lazy
  88. from django.views.generic.edit import CreateView, DeleteView, UpdateView
  89. from myapp.models import Author
  90. class AuthorCreateView(CreateView):
  91. model = Author
  92. fields = ["name"]
  93. class AuthorUpdateView(UpdateView):
  94. model = Author
  95. fields = ["name"]
  96. class AuthorDeleteView(DeleteView):
  97. model = Author
  98. success_url = reverse_lazy("author-list")
  99. .. note::
  100. We have to use :func:`~django.urls.reverse_lazy` instead of
  101. ``reverse()``, as the urls are not loaded when the file is imported.
  102. The ``fields`` attribute works the same way as the ``fields`` attribute on the
  103. inner ``Meta`` class on :class:`~django.forms.ModelForm`. Unless you define the
  104. form class in another way, the attribute is required and the view will raise
  105. an :exc:`~django.core.exceptions.ImproperlyConfigured` exception if it's not.
  106. If you specify both the :attr:`~django.views.generic.edit.ModelFormMixin.fields`
  107. and :attr:`~django.views.generic.edit.FormMixin.form_class` attributes, an
  108. :exc:`~django.core.exceptions.ImproperlyConfigured` exception will be raised.
  109. Finally, we hook these new views into the URLconf:
  110. .. code-block:: python
  111. :caption: ``urls.py``
  112. from django.urls import path
  113. from myapp.views import AuthorCreateView, AuthorDeleteView, AuthorUpdateView
  114. urlpatterns = [
  115. # ...
  116. path("author/add/", AuthorCreateView.as_view(), name="author-add"),
  117. path("author/<int:pk>/", AuthorUpdateView.as_view(), name="author-update"),
  118. path("author/<int:pk>/delete/", AuthorDeleteView.as_view(), name="author-delete"),
  119. ]
  120. .. note::
  121. These views inherit
  122. :class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
  123. which uses
  124. :attr:`~django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix`
  125. to construct the
  126. :attr:`~django.views.generic.base.TemplateResponseMixin.template_name`
  127. based on the model.
  128. In this example:
  129. * :class:`CreateView` and :class:`UpdateView` use ``myapp/author_form.html``
  130. * :class:`DeleteView` uses ``myapp/author_confirm_delete.html``
  131. If you wish to have separate templates for :class:`CreateView` and
  132. :class:`UpdateView`, you can set either
  133. :attr:`~django.views.generic.base.TemplateResponseMixin.template_name` or
  134. :attr:`~django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix`
  135. on your view class.
  136. Models and ``request.user``
  137. ===========================
  138. To track the user that created an object using a :class:`CreateView`,
  139. you can use a custom :class:`~django.forms.ModelForm` to do this. First, add
  140. the foreign key relation to the model:
  141. .. code-block:: python
  142. :caption: ``models.py``
  143. from django.contrib.auth.models import User
  144. from django.db import models
  145. class Author(models.Model):
  146. name = models.CharField(max_length=200)
  147. created_by = models.ForeignKey(User, on_delete=models.CASCADE)
  148. # ...
  149. In the view, ensure that you don't include ``created_by`` in the list of fields
  150. to edit, and override
  151. :meth:`~django.views.generic.edit.ModelFormMixin.form_valid()` to add the user:
  152. .. code-block:: python
  153. :caption: ``views.py``
  154. from django.contrib.auth.mixins import LoginRequiredMixin
  155. from django.views.generic.edit import CreateView
  156. from myapp.models import Author
  157. class AuthorCreateView(LoginRequiredMixin, CreateView):
  158. model = Author
  159. fields = ["name"]
  160. def form_valid(self, form):
  161. form.instance.created_by = self.request.user
  162. return super().form_valid(form)
  163. :class:`~django.contrib.auth.mixins.LoginRequiredMixin` prevents users who
  164. aren't logged in from accessing the form. If you omit that, you'll need to
  165. handle unauthorized users in :meth:`~.ModelFormMixin.form_valid()`.
  166. .. _content-negotiation-example:
  167. Content negotiation example
  168. ===========================
  169. Here is an example showing how you might go about implementing a form that
  170. works with an API-based workflow as well as 'normal' form POSTs::
  171. from django.http import JsonResponse
  172. from django.views.generic.edit import CreateView
  173. from myapp.models import Author
  174. class JsonableResponseMixin:
  175. """
  176. Mixin to add JSON support to a form.
  177. Must be used with an object-based FormView (e.g. CreateView)
  178. """
  179. def form_invalid(self, form):
  180. response = super().form_invalid(form)
  181. if self.request.accepts("text/html"):
  182. return response
  183. else:
  184. return JsonResponse(form.errors, status=400)
  185. def form_valid(self, form):
  186. # We make sure to call the parent's form_valid() method because
  187. # it might do some processing (in the case of CreateView, it will
  188. # call form.save() for example).
  189. response = super().form_valid(form)
  190. if self.request.accepts("text/html"):
  191. return response
  192. else:
  193. data = {
  194. "pk": self.object.pk,
  195. }
  196. return JsonResponse(data)
  197. class AuthorCreateView(JsonableResponseMixin, CreateView):
  198. model = Author
  199. fields = ["name"]
  200. The above example assumes that if the client supports ``text/html``, that they
  201. would prefer it. However, this may not always be true. When requesting a
  202. ``.css`` file, many browsers will send the header
  203. ``Accept: text/css,*/*;q=0.1``, indicating that they would prefer CSS, but
  204. anything else is fine. This means ``request.accepts("text/html")`` will be
  205. ``True``.
  206. To determine the correct format, taking into consideration the client's
  207. preference, use :func:`django.http.HttpRequest.get_preferred_type`::
  208. class JsonableResponseMixin:
  209. """
  210. Mixin to add JSON support to a form.
  211. Must be used with an object-based FormView (e.g. CreateView).
  212. """
  213. accepted_media_types = ["text/html", "application/json"]
  214. def dispatch(self, request, *args, **kwargs):
  215. if request.get_preferred_type(self.accepted_media_types) is None:
  216. # No format in common.
  217. return HttpResponse(
  218. status_code=406, headers={"Accept": ",".join(self.accepted_media_types)}
  219. )
  220. return super().dispatch(request, *args, **kwargs)
  221. def form_invalid(self, form):
  222. response = super().form_invalid(form)
  223. accepted_type = request.get_preferred_type(self.accepted_media_types)
  224. if accepted_type == "text/html":
  225. return response
  226. elif accepted_type == "application/json":
  227. return JsonResponse(form.errors, status=400)
  228. def form_valid(self, form):
  229. # We make sure to call the parent's form_valid() method because
  230. # it might do some processing (in the case of CreateView, it will
  231. # call form.save() for example).
  232. response = super().form_valid(form)
  233. accepted_type = request.get_preferred_type(self.accepted_media_types)
  234. if accepted_type == "text/html":
  235. return response
  236. elif accepted_type == "application/json":
  237. data = {
  238. "pk": self.object.pk,
  239. }
  240. return JsonResponse(data)
  241. .. versionchanged:: 5.2
  242. The :meth:`.HttpRequest.get_preferred_type` method was added.