2
0

generic-editing.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. =====================
  2. Generic editing views
  3. =====================
  4. The following views are described on this page and provide a foundation for
  5. editing content:
  6. * :class:`django.views.generic.edit.FormView`
  7. * :class:`django.views.generic.edit.CreateView`
  8. * :class:`django.views.generic.edit.UpdateView`
  9. * :class:`django.views.generic.edit.DeleteView`
  10. .. seealso::
  11. The :doc:`messages framework </ref/contrib/messages>` contains
  12. :class:`~django.contrib.messages.views.SuccessMessageMixin`, which
  13. facilitates presenting messages about successful form submissions.
  14. .. note::
  15. Some of the examples on this page assume that an ``Author`` model has been
  16. defined as follows in ``myapp/models.py``::
  17. from django.db import models
  18. from django.urls import reverse
  19. class Author(models.Model):
  20. name = models.CharField(max_length=200)
  21. def get_absolute_url(self):
  22. return reverse("author-detail", kwargs={"pk": self.pk})
  23. ``FormView``
  24. ============
  25. .. class:: django.views.generic.edit.FormView
  26. A view that displays a form. On error, redisplays the form with validation
  27. errors; on success, redirects to a new URL.
  28. **Ancestors (MRO)**
  29. This view inherits methods and attributes from the following views:
  30. * :class:`django.views.generic.base.TemplateResponseMixin`
  31. * :class:`django.views.generic.edit.BaseFormView`
  32. * :class:`django.views.generic.edit.FormMixin`
  33. * :class:`django.views.generic.edit.ProcessFormView`
  34. * :class:`django.views.generic.base.View`
  35. **Example myapp/forms.py**::
  36. from django import forms
  37. class ContactForm(forms.Form):
  38. name = forms.CharField()
  39. message = forms.CharField(widget=forms.Textarea)
  40. def send_email(self):
  41. # send email using the self.cleaned_data dictionary
  42. pass
  43. **Example myapp/views.py**::
  44. from myapp.forms import ContactForm
  45. from django.views.generic.edit import FormView
  46. class ContactFormView(FormView):
  47. template_name = "contact.html"
  48. form_class = ContactForm
  49. success_url = "/thanks/"
  50. def form_valid(self, form):
  51. # This method is called when valid form data has been POSTed.
  52. # It should return an HttpResponse.
  53. form.send_email()
  54. return super().form_valid(form)
  55. **Example myapp/contact.html**:
  56. .. code-block:: html+django
  57. <form method="post">{% csrf_token %}
  58. {{ form.as_p }}
  59. <input type="submit" value="Send message">
  60. </form>
  61. .. class:: django.views.generic.edit.BaseFormView
  62. A base view for displaying a form. It is not intended to be used directly,
  63. but rather as a parent class of the
  64. :class:`django.views.generic.edit.FormView` or other views displaying a
  65. form.
  66. **Ancestors (MRO)**
  67. This view inherits methods and attributes from the following views:
  68. * :class:`django.views.generic.edit.FormMixin`
  69. * :class:`django.views.generic.edit.ProcessFormView`
  70. ``CreateView``
  71. ==============
  72. .. class:: django.views.generic.edit.CreateView
  73. A view that displays a form for creating an object, redisplaying the form
  74. with validation errors (if there are any) and saving the object.
  75. **Ancestors (MRO)**
  76. This view inherits methods and attributes from the following views:
  77. * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
  78. * :class:`django.views.generic.base.TemplateResponseMixin`
  79. * :class:`django.views.generic.edit.BaseCreateView`
  80. * :class:`django.views.generic.edit.ModelFormMixin`
  81. * :class:`django.views.generic.edit.FormMixin`
  82. * :class:`django.views.generic.detail.SingleObjectMixin`
  83. * :class:`django.views.generic.edit.ProcessFormView`
  84. * :class:`django.views.generic.base.View`
  85. **Attributes**
  86. .. attribute:: template_name_suffix
  87. The ``CreateView`` page displayed to a ``GET`` request uses a
  88. ``template_name_suffix`` of ``'_form'``. For
  89. example, changing this attribute to ``'_create_form'`` for a view
  90. creating objects for the example ``Author`` model would cause the
  91. default ``template_name`` to be ``'myapp/author_create_form.html'``.
  92. .. attribute:: object
  93. When using ``CreateView`` you have access to ``self.object``, which is
  94. the object being created. If the object hasn't been created yet, the
  95. value will be ``None``.
  96. **Example myapp/views.py**::
  97. from django.views.generic.edit import CreateView
  98. from myapp.models import Author
  99. class AuthorCreateView(CreateView):
  100. model = Author
  101. fields = ["name"]
  102. **Example myapp/author_form.html**:
  103. .. code-block:: html+django
  104. <form method="post">{% csrf_token %}
  105. {{ form.as_p }}
  106. <input type="submit" value="Save">
  107. </form>
  108. .. class:: django.views.generic.edit.BaseCreateView
  109. A base view for creating a new object instance. It is not intended to be
  110. used directly, but rather as a parent class of the
  111. :class:`django.views.generic.edit.CreateView`.
  112. **Ancestors (MRO)**
  113. This view inherits methods and attributes from the following views:
  114. * :class:`django.views.generic.edit.ModelFormMixin`
  115. * :class:`django.views.generic.edit.ProcessFormView`
  116. **Methods**
  117. .. method:: get(request, *args, **kwargs)
  118. Sets the current object instance (``self.object``) to ``None``.
  119. .. method:: post(request, *args, **kwargs)
  120. Sets the current object instance (``self.object``) to ``None``.
  121. ``UpdateView``
  122. ==============
  123. .. class:: django.views.generic.edit.UpdateView
  124. A view that displays a form for editing an existing object, redisplaying
  125. the form with validation errors (if there are any) and saving changes to
  126. the object. This uses a form automatically generated from the object's
  127. model class (unless a form class is manually specified).
  128. **Ancestors (MRO)**
  129. This view inherits methods and attributes from the following views:
  130. * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
  131. * :class:`django.views.generic.base.TemplateResponseMixin`
  132. * :class:`django.views.generic.edit.BaseUpdateView`
  133. * :class:`django.views.generic.edit.ModelFormMixin`
  134. * :class:`django.views.generic.edit.FormMixin`
  135. * :class:`django.views.generic.detail.SingleObjectMixin`
  136. * :class:`django.views.generic.edit.ProcessFormView`
  137. * :class:`django.views.generic.base.View`
  138. **Attributes**
  139. .. attribute:: template_name_suffix
  140. The ``UpdateView`` page displayed to a ``GET`` request uses a
  141. ``template_name_suffix`` of ``'_form'``. For
  142. example, changing this attribute to ``'_update_form'`` for a view
  143. updating objects for the example ``Author`` model would cause the
  144. default ``template_name`` to be ``'myapp/author_update_form.html'``.
  145. .. attribute:: object
  146. When using ``UpdateView`` you have access to ``self.object``, which is
  147. the object being updated.
  148. **Example myapp/views.py**::
  149. from django.views.generic.edit import UpdateView
  150. from myapp.models import Author
  151. class AuthorUpdateView(UpdateView):
  152. model = Author
  153. fields = ["name"]
  154. template_name_suffix = "_update_form"
  155. **Example myapp/author_update_form.html**:
  156. .. code-block:: html+django
  157. <form method="post">{% csrf_token %}
  158. {{ form.as_p }}
  159. <input type="submit" value="Update">
  160. </form>
  161. .. class:: django.views.generic.edit.BaseUpdateView
  162. A base view for updating an existing object instance. It is not intended to
  163. be used directly, but rather as a parent class of the
  164. :class:`django.views.generic.edit.UpdateView`.
  165. **Ancestors (MRO)**
  166. This view inherits methods and attributes from the following views:
  167. * :class:`django.views.generic.edit.ModelFormMixin`
  168. * :class:`django.views.generic.edit.ProcessFormView`
  169. **Methods**
  170. .. method:: get(request, *args, **kwargs)
  171. Sets the current object instance (``self.object``).
  172. .. method:: post(request, *args, **kwargs)
  173. Sets the current object instance (``self.object``).
  174. ``DeleteView``
  175. ==============
  176. .. class:: django.views.generic.edit.DeleteView
  177. A view that displays a confirmation page and deletes an existing object.
  178. The given object will only be deleted if the request method is ``POST``. If
  179. this view is fetched via ``GET``, it will display a confirmation page that
  180. should contain a form that POSTs to the same URL.
  181. **Ancestors (MRO)**
  182. This view inherits methods and attributes from the following views:
  183. * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
  184. * :class:`django.views.generic.base.TemplateResponseMixin`
  185. * :class:`django.views.generic.edit.BaseDeleteView`
  186. * :class:`django.views.generic.edit.DeletionMixin`
  187. * :class:`django.views.generic.edit.FormMixin`
  188. * :class:`django.views.generic.base.ContextMixin`
  189. * :class:`django.views.generic.detail.BaseDetailView`
  190. * :class:`django.views.generic.detail.SingleObjectMixin`
  191. * :class:`django.views.generic.base.View`
  192. **Attributes**
  193. .. attribute:: form_class
  194. Inherited from :class:`~django.views.generic.edit.BaseDeleteView`. The
  195. form class that will be used to confirm the request. By default
  196. :class:`django.forms.Form`, resulting in an empty form that is always
  197. valid.
  198. By providing your own ``Form`` subclass, you can add additional
  199. requirements, such as a confirmation checkbox, for example.
  200. .. attribute:: template_name_suffix
  201. The ``DeleteView`` page displayed to a ``GET`` request uses a
  202. ``template_name_suffix`` of ``'_confirm_delete'``. For
  203. example, changing this attribute to ``'_check_delete'`` for a view
  204. deleting objects for the example ``Author`` model would cause the
  205. default ``template_name`` to be ``'myapp/author_check_delete.html'``.
  206. **Example myapp/views.py**::
  207. from django.urls import reverse_lazy
  208. from django.views.generic.edit import DeleteView
  209. from myapp.models import Author
  210. class AuthorDeleteView(DeleteView):
  211. model = Author
  212. success_url = reverse_lazy("author-list")
  213. **Example myapp/author_confirm_delete.html**:
  214. .. code-block:: html+django
  215. <form method="post">{% csrf_token %}
  216. <p>Are you sure you want to delete "{{ object }}"?</p>
  217. {{ form }}
  218. <input type="submit" value="Confirm">
  219. </form>
  220. .. class:: django.views.generic.edit.BaseDeleteView
  221. A base view for deleting an object instance. It is not intended to be used
  222. directly, but rather as a parent class of the
  223. :class:`django.views.generic.edit.DeleteView`.
  224. **Ancestors (MRO)**
  225. This view inherits methods and attributes from the following views:
  226. * :class:`django.views.generic.edit.DeletionMixin`
  227. * :class:`django.views.generic.edit.FormMixin`
  228. * :class:`django.views.generic.detail.BaseDetailView`