generic-editing.txt 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 simple 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 ContactView(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) simply 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 AuthorCreate(CreateView):
  91. model = Author
  92. fields = ['name']
  93. class AuthorUpdate(UpdateView):
  94. model = Author
  95. fields = ['name']
  96. class AuthorDelete(DeleteView):
  97. model = Author
  98. success_url = reverse_lazy('author-list')
  99. .. note::
  100. We have to use :func:`~django.urls.reverse_lazy` here, not just
  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 AuthorCreate, AuthorDelete, AuthorUpdate
  114. urlpatterns = [
  115. # ...
  116. path('author/add/', AuthorCreate.as_view(), name='author-add'),
  117. path('author/<int:pk>/', AuthorUpdate.as_view(), name='author-update'),
  118. path('author/<int:pk>/delete/', AuthorDelete.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.views.generic.edit import CreateView
  155. from myapp.models import Author
  156. class AuthorCreate(CreateView):
  157. model = Author
  158. fields = ['name']
  159. def form_valid(self, form):
  160. form.instance.created_by = self.request.user
  161. return super().form_valid(form)
  162. Note that you'll need to :ref:`decorate this
  163. view<decorating-class-based-views>` using
  164. :func:`~django.contrib.auth.decorators.login_required`, or
  165. alternatively handle unauthorized users in the
  166. :meth:`~django.views.generic.edit.ModelFormMixin.form_valid()`.
  167. AJAX example
  168. ============
  169. Here is a simple example showing how you might go about implementing a form that
  170. works for AJAX requests 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 AjaxableResponseMixin:
  175. """
  176. Mixin to add AJAX 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.is_ajax():
  182. return JsonResponse(form.errors, status=400)
  183. else:
  184. return response
  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.is_ajax():
  191. data = {
  192. 'pk': self.object.pk,
  193. }
  194. return JsonResponse(data)
  195. else:
  196. return response
  197. class AuthorCreate(AjaxableResponseMixin, CreateView):
  198. model = Author
  199. fields = ['name']