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