generic-editing.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. .. note::
  11. Some of the examples on this page assume that a model titled 'Author'
  12. has been defined. For these cases we assume the following has been defined
  13. in `myapp/models.py`::
  14. from django import models
  15. from django.core.urlresolvers import reverse
  16. class Author(models.Model):
  17. name = models.CharField(max_length=200)
  18. def get_absolute_url(self):
  19. return reverse('author-detail', kwargs={'pk': self.pk})
  20. .. class:: django.views.generic.edit.FormView
  21. A view that displays a form. On error, redisplays the form with validation
  22. errors; on success, redirects to a new URL.
  23. **Ancestors (MRO)**
  24. This view inherits methods and attributes from the following views:
  25. * :class:`django.views.generic.edit.FormView`
  26. * :class:`django.views.generic.base.TemplateResponseMixin`
  27. * :class:`django.views.generic.edit.BaseFormView`
  28. * :class:`django.views.generic.edit.FormMixin`
  29. * :class:`django.views.generic.edit.ProcessFormView`
  30. * :class:`django.views.generic.base.View`
  31. **Example forms.py**::
  32. from django import forms
  33. class ContactForm(forms.Form):
  34. name = forms.CharField()
  35. message = forms.CharField(widget=forms.Textarea)
  36. def send_email(self):
  37. # send email using the self.cleaned_data dictionary
  38. pass
  39. **Example views.py**::
  40. from myapp.forms import ContactForm
  41. from django.views.generic.edit import FormView
  42. class ContactView(FormView):
  43. template_name = 'contact.html'
  44. form_class = ContactForm
  45. success_url = '/thanks/'
  46. def form_valid(self, form):
  47. # This method is called when valid form data has been POSTed.
  48. # It should return an HttpResponse.
  49. form.send_email()
  50. return super(ContactView, self).form_valid(form)
  51. .. class:: django.views.generic.edit.CreateView
  52. A view that displays a form for creating an object, redisplaying the form
  53. with validation errors (if there are any) and saving the object.
  54. **Ancestors (MRO)**
  55. This view inherits methods and attributes from the following views:
  56. * :class:`django.views.generic.edit.CreateView`
  57. * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
  58. * :class:`django.views.generic.base.TemplateResponseMixin`
  59. * :class:`django.views.generic.edit.BaseCreateView`
  60. * :class:`django.views.generic.edit.ModelFormMixin`
  61. * :class:`django.views.generic.edit.FormMixin`
  62. * :class:`django.views.generic.detail.SingleObjectMixin`
  63. * :class:`django.views.generic.edit.ProcessFormView`
  64. * :class:`django.views.generic.base.View`
  65. **Attributes**
  66. .. attribute:: template_name_suffix
  67. The CreateView page displayed to a GET request uses a
  68. ``template_name_suffix`` of ``'_form.html'``. For
  69. example, changing this attribute to ``'_create_form.html'`` for a view
  70. creating objects for the the example `Author` model would cause the the
  71. default `template_name` to be ``'myapp/author_create_form.html'``.
  72. **Example views.py**::
  73. from django.views.generic.edit import CreateView
  74. from myapp.models import Author
  75. class AuthorCreate(CreateView):
  76. model = Author
  77. .. class:: django.views.generic.edit.UpdateView
  78. A view that displays a form for editing an existing object, redisplaying
  79. the form with validation errors (if there are any) and saving changes to
  80. the object. This uses a form automatically generated from the object's
  81. model class (unless a form class is manually specified).
  82. **Ancestors (MRO)**
  83. This view inherits methods and attributes from the following views:
  84. * :class:`django.views.generic.edit.UpdateView`
  85. * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
  86. * :class:`django.views.generic.base.TemplateResponseMixin`
  87. * :class:`django.views.generic.edit.BaseUpdateView`
  88. * :class:`django.views.generic.edit.ModelFormMixin`
  89. * :class:`django.views.generic.edit.FormMixin`
  90. * :class:`django.views.generic.detail.SingleObjectMixin`
  91. * :class:`django.views.generic.edit.ProcessFormView`
  92. * :class:`django.views.generic.base.View`
  93. **Attributes**
  94. .. attribute:: template_name_suffix
  95. The UpdateView page displayed to a GET request uses a
  96. ``template_name_suffix`` of ``'_form.html'``. For
  97. example, changing this attribute to ``'_update_form.html'`` for a view
  98. updating objects for the the example `Author` model would cause the the
  99. default `template_name` to be ``'myapp/author_update_form.html'``.
  100. **Example views.py**::
  101. from django.views.generic.edit import UpdateView
  102. from myapp.models import Author
  103. class AuthorUpdate(UpdateView):
  104. model = Author
  105. .. class:: django.views.generic.edit.DeleteView
  106. A view that displays a confirmation page and deletes an existing object.
  107. The given object will only be deleted if the request method is ``POST``. If
  108. this view is fetched via ``GET``, it will display a confirmation page that
  109. should contain a form that POSTs to the same URL.
  110. **Ancestors (MRO)**
  111. This view inherits methods and attributes from the following views:
  112. * :class:`django.views.generic.edit.DeleteView`
  113. * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
  114. * :class:`django.views.generic.base.TemplateResponseMixin`
  115. * :class:`django.views.generic.edit.BaseDeleteView`
  116. * :class:`django.views.generic.edit.DeletionMixin`
  117. * :class:`django.views.generic.detail.BaseDetailView`
  118. * :class:`django.views.generic.detail.SingleObjectMixin`
  119. * :class:`django.views.generic.base.View`
  120. **Attributes**
  121. .. attribute:: template_name_suffix
  122. The DeleteView page displayed to a GET request uses a
  123. ``template_name_suffix`` of ``'_confirm_delete.html'``. For
  124. example, changing this attribute to ``'_check_delete.html'`` for a view
  125. deleting objects for the the example `Author` model would cause the the
  126. default `template_name` to be ``'myapp/author_check_delete.html'``.
  127. **Example views.py**::
  128. from django.views.generic.edit import DeleteView
  129. from django.core.urlresolvers import reverse_lazy
  130. from myapp.models import Author
  131. class AuthorDelete(DeleteView):
  132. model = Author
  133. success_url = reverse_lazy('author-list')