generic-editing.txt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. Form handling with class-based views
  2. ====================================
  3. Form processing generally has 3 paths:
  4. * Initial GET (blank or prepopulated form)
  5. * POST with invalid data (typically redisplay form with errors)
  6. * POST with valid data (process the data and typically redirect)
  7. Implementing this yourself often results in a lot of repeated
  8. boilerplate code (see :ref:`Using a form in a
  9. view<using-a-form-in-a-view>`). To help avoid this, Django provides a
  10. collection of generic class-based views for form processing.
  11. Basic Forms
  12. -----------
  13. Given a simple contact form::
  14. # forms.py
  15. from django import forms
  16. class ContactForm(forms.Form):
  17. name = forms.CharField()
  18. message = forms.CharField(widget=forms.Textarea)
  19. def send_email(self):
  20. # send email using the self.cleaned_data dictionary
  21. pass
  22. The view can be constructed using a FormView::
  23. # views.py
  24. from myapp.forms import ContactForm
  25. from django.views.generic.edit import FormView
  26. class ContactView(FormView):
  27. template_name = 'contact.html'
  28. form_class = ContactForm
  29. success_url = '/thanks/'
  30. def form_valid(self, form):
  31. # This method is called when valid form data has been POSTed.
  32. # It should return an HttpResponse.
  33. form.send_email()
  34. return super(ContactView, self).form_valid(form)
  35. Notes:
  36. * FormView inherits
  37. :class:`~django.views.generic.base.TemplateResponseMixin` so
  38. :attr:`~django.views.generic.base.TemplateResponseMixin.template_name`
  39. can be used here
  40. * The default implementation for
  41. :meth:`~django.views.generic.edit.FormView.form_valid` simply
  42. redirects to the :attr:`success_url`
  43. Model Forms
  44. -----------
  45. Generic views really shine when working with models. These generic
  46. views will automatically create a :class:`ModelForm`, so long as they
  47. can work out which model class to use:
  48. * If the :attr:`model` attribute is given, that model class will be used
  49. * If :meth:`get_object()` returns an object, the class of that object
  50. will be used
  51. * If a :attr:`queryset` is given, the model for that queryset will be used
  52. Model form views provide a :meth:`form_valid()` implementation that
  53. saves the model automatically. You can override this if you have any
  54. special requirements; see below for examples.
  55. You don't even need to provide a attr:`success_url` for
  56. :class:`~django.views.generic.edit.CreateView` or
  57. :class:`~django.views.generic.edit.UpdateView` - they will use
  58. :meth:`get_absolute_url()` on the model object if available.
  59. If you want to use a custom :class:`ModelForm` (for instance to add
  60. extra validation) simply set
  61. :attr:`~django.views.generic.edit.FormMixin.form_class` on your view.
  62. .. note::
  63. When specifying a custom form class, you must still specify the model,
  64. even though the :attr:`form_class` may be a :class:`ModelForm`.
  65. First we need to add :meth:`get_absolute_url()` to our :class:`Author`
  66. class:
  67. .. code-block:: python
  68. # models.py
  69. from django import models
  70. from django.core.urlresolvers import reverse
  71. class Author(models.Model):
  72. name = models.CharField(max_length=200)
  73. def get_absolute_url(self):
  74. return reverse('author-detail', kwargs={'pk': self.pk})
  75. Then we can use :class:`CreateView` and friends to do the actual
  76. work. Notice how we're just configuring the generic class-based views
  77. here; we don't have to write any logic ourselves::
  78. # views.py
  79. from django.views.generic.edit import CreateView, UpdateView, DeleteView
  80. from django.core.urlresolvers import reverse_lazy
  81. from myapp.models import Author
  82. class AuthorCreate(CreateView):
  83. model = Author
  84. class AuthorUpdate(UpdateView):
  85. model = Author
  86. class AuthorDelete(DeleteView):
  87. model = Author
  88. success_url = reverse_lazy('author-list')
  89. .. note::
  90. We have to use :func:`~django.core.urlresolvers.reverse_lazy` here, not
  91. just ``reverse`` as the urls are not loaded when the file is imported.
  92. Finally, we hook these new views into the URLconf::
  93. # urls.py
  94. from django.conf.urls import patterns, url
  95. from myapp.views import AuthorCreate, AuthorUpdate, AuthorDelete
  96. urlpatterns = patterns('',
  97. # ...
  98. url(r'author/add/$', AuthorCreate.as_view(), name='author_add'),
  99. url(r'author/(?P<pk>\d+)/$', AuthorUpdate.as_view(), name='author_update'),
  100. url(r'author/(?P<pk>\d+)/delete/$', AuthorDelete.as_view(), name='author_delete'),
  101. )
  102. .. note::
  103. These views inherit :class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
  104. which uses :attr:`~django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_prefix`
  105. to construct the
  106. :attr:`~django.views.generic.base.TemplateResponseMixin.template_name`
  107. based on the model.
  108. In this example:
  109. * :class:`CreateView` and :class:`UpdateView` use ``myapp/author_form.html``
  110. * :class:`DeleteView` uses ``myapp/author_confirm_delete.html``
  111. If you wish to have separate templates for :class:`CreateView` and
  112. :class:1UpdateView`, you can set either :attr:`template_name` or
  113. :attr:`template_name_suffix` on your view class.
  114. Models and request.user
  115. -----------------------
  116. To track the user that created an object using a :class:`CreateView`,
  117. you can use a custom :class:`ModelForm` to do this. First, add the
  118. foreign key relation to the model::
  119. # models.py
  120. from django import models
  121. from django.contrib.auth import User
  122. class Author(models.Model):
  123. name = models.CharField(max_length=200)
  124. created_by = models.ForeignKey(User)
  125. # ...
  126. Create a custom :class:`ModelForm` in order to exclude the
  127. ``created_by`` field and prevent the user from editing it:
  128. .. code-block:: python
  129. # forms.py
  130. from django import forms
  131. from myapp.models import Author
  132. class AuthorForm(forms.ModelForm):
  133. class Meta:
  134. model = Author
  135. exclude = ('created_by',)
  136. In the view, use the custom :attr:`form_class` and override
  137. :meth:`form_valid()` to add the user::
  138. # views.py
  139. from django.views.generic.edit import CreateView
  140. from myapp.models import Author
  141. from myapp.forms import AuthorForm
  142. class AuthorCreate(CreateView):
  143. form_class = AuthorForm
  144. model = Author
  145. def form_valid(self, form):
  146. form.instance.created_by = self.request.user
  147. return super(AuthorCreate, self).form_valid(form)
  148. Note that you'll need to :ref:`decorate this
  149. view<decorating-class-based-views>` using
  150. :func:`~django.contrib.auth.decorators.login_required`, or
  151. alternatively handle unauthorised users in the :meth:`form_valid()`.