2012-06-11 16:34:00 +08:00
|
|
|
Form handling with class-based views
|
|
|
|
====================================
|
|
|
|
|
|
|
|
Form processing generally has 3 paths:
|
|
|
|
|
|
|
|
* Initial GET (blank or prepopulated form)
|
|
|
|
* POST with invalid data (typically redisplay form with errors)
|
|
|
|
* POST with valid data (process the data and typically redirect)
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
Implementing this yourself often results in a lot of repeated boilerplate code
|
|
|
|
(see :ref:`Using a form in a view<using-a-form-in-a-view>`). To help avoid
|
|
|
|
this, Django provides a collection of generic class-based views for form
|
|
|
|
processing.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
Basic Forms
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Given a simple contact form::
|
|
|
|
|
|
|
|
# forms.py
|
|
|
|
from django import forms
|
|
|
|
|
|
|
|
class ContactForm(forms.Form):
|
|
|
|
name = forms.CharField()
|
|
|
|
message = forms.CharField(widget=forms.Textarea)
|
|
|
|
|
|
|
|
def send_email(self):
|
|
|
|
# send email using the self.cleaned_data dictionary
|
|
|
|
pass
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
The view can be constructed using a ``FormView``::
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
# views.py
|
|
|
|
from myapp.forms import ContactForm
|
|
|
|
from django.views.generic.edit import FormView
|
|
|
|
|
|
|
|
class ContactView(FormView):
|
|
|
|
template_name = 'contact.html'
|
|
|
|
form_class = ContactForm
|
|
|
|
success_url = '/thanks/'
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
# This method is called when valid form data has been POSTed.
|
|
|
|
# It should return an HttpResponse.
|
|
|
|
form.send_email()
|
|
|
|
return super(ContactView, self).form_valid(form)
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
|
|
|
* FormView inherits
|
|
|
|
:class:`~django.views.generic.base.TemplateResponseMixin` so
|
|
|
|
:attr:`~django.views.generic.base.TemplateResponseMixin.template_name`
|
2013-01-01 21:12:42 +08:00
|
|
|
can be used here.
|
2012-06-11 16:34:00 +08:00
|
|
|
* The default implementation for
|
2013-01-01 21:12:42 +08:00
|
|
|
:meth:`~django.views.generic.edit.FormMixin.form_valid` simply
|
|
|
|
redirects to the :attr:`~django.views.generic.edit.FormMixin.success_url`.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
Model Forms
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Generic views really shine when working with models. These generic
|
2013-01-01 21:12:42 +08:00
|
|
|
views will automatically create a :class:`~django.forms.ModelForm`, so long as
|
|
|
|
they can work out which model class to use:
|
|
|
|
|
|
|
|
* If the :attr:`~django.views.generic.edit.ModelFormMixin.model` attribute is
|
|
|
|
given, that model class will be used.
|
|
|
|
* If :meth:`~django.views.generic.detail.SingleObjectMixin.get_object()`
|
|
|
|
returns an object, the class of that object will be used.
|
|
|
|
* If a :attr:`~django.views.generic.detail.SingleObjectMixin.queryset` is
|
|
|
|
given, the model for that queryset will be used.
|
|
|
|
|
|
|
|
Model form views provide a
|
|
|
|
:meth:`~django.views.generic.edit.ModelFormMixin.form_valid()` implementation
|
|
|
|
that saves the model automatically. You can override this if you have any
|
2012-06-11 16:34:00 +08:00
|
|
|
special requirements; see below for examples.
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
You don't even need to provide a ``success_url`` for
|
2012-06-11 16:34:00 +08:00
|
|
|
:class:`~django.views.generic.edit.CreateView` or
|
|
|
|
:class:`~django.views.generic.edit.UpdateView` - they will use
|
2013-01-01 21:12:42 +08:00
|
|
|
:meth:`~django.db.models.Model.get_absolute_url()` on the model object if available.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
If you want to use a custom :class:`~django.forms.ModelForm` (for instance to
|
|
|
|
add extra validation) simply set
|
2012-06-11 16:34:00 +08:00
|
|
|
:attr:`~django.views.generic.edit.FormMixin.form_class` on your view.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
When specifying a custom form class, you must still specify the model,
|
2013-01-01 21:12:42 +08:00
|
|
|
even though the :attr:`~django.views.generic.edit.FormMixin.form_class` may
|
|
|
|
be a :class:`~django.forms.ModelForm`.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
First we need to add :meth:`~django.db.models.Model.get_absolute_url()` to our
|
|
|
|
``Author`` class:
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# models.py
|
|
|
|
from django.core.urlresolvers import reverse
|
2012-11-14 03:46:29 +08:00
|
|
|
from django.db import models
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
class Author(models.Model):
|
|
|
|
name = models.CharField(max_length=200)
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('author-detail', kwargs={'pk': self.pk})
|
|
|
|
|
|
|
|
Then we can use :class:`CreateView` and friends to do the actual
|
|
|
|
work. Notice how we're just configuring the generic class-based views
|
|
|
|
here; we don't have to write any logic ourselves::
|
2012-11-14 03:46:29 +08:00
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
# views.py
|
|
|
|
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
|
|
|
from django.core.urlresolvers import reverse_lazy
|
|
|
|
from myapp.models import Author
|
|
|
|
|
|
|
|
class AuthorCreate(CreateView):
|
|
|
|
model = Author
|
|
|
|
|
|
|
|
class AuthorUpdate(UpdateView):
|
|
|
|
model = Author
|
|
|
|
|
|
|
|
class AuthorDelete(DeleteView):
|
|
|
|
model = Author
|
|
|
|
success_url = reverse_lazy('author-list')
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
We have to use :func:`~django.core.urlresolvers.reverse_lazy` here, not
|
|
|
|
just ``reverse`` as the urls are not loaded when the file is imported.
|
|
|
|
|
|
|
|
Finally, we hook these new views into the URLconf::
|
|
|
|
|
|
|
|
# urls.py
|
|
|
|
from django.conf.urls import patterns, url
|
|
|
|
from myapp.views import AuthorCreate, AuthorUpdate, AuthorDelete
|
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
|
|
|
# ...
|
|
|
|
url(r'author/add/$', AuthorCreate.as_view(), name='author_add'),
|
|
|
|
url(r'author/(?P<pk>\d+)/$', AuthorUpdate.as_view(), name='author_update'),
|
|
|
|
url(r'author/(?P<pk>\d+)/delete/$', AuthorDelete.as_view(), name='author_delete'),
|
|
|
|
)
|
2012-11-14 03:46:29 +08:00
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
.. note::
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
These views inherit
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
|
|
|
|
which uses
|
|
|
|
:attr:`~django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix`
|
2012-06-11 16:34:00 +08:00
|
|
|
to construct the
|
|
|
|
:attr:`~django.views.generic.base.TemplateResponseMixin.template_name`
|
|
|
|
based on the model.
|
|
|
|
|
|
|
|
In this example:
|
|
|
|
|
|
|
|
* :class:`CreateView` and :class:`UpdateView` use ``myapp/author_form.html``
|
|
|
|
* :class:`DeleteView` uses ``myapp/author_confirm_delete.html``
|
|
|
|
|
|
|
|
If you wish to have separate templates for :class:`CreateView` and
|
2013-01-01 21:12:42 +08:00
|
|
|
:class:`UpdateView`, you can set either
|
|
|
|
:attr:`~django.views.generic.base.TemplateResponseMixin.template_name` or
|
|
|
|
:attr:`~django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix`
|
|
|
|
on your view class.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
Models and request.user
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
To track the user that created an object using a :class:`CreateView`,
|
2013-01-01 21:12:42 +08:00
|
|
|
you can use a custom :class:`~django.forms.ModelForm` to do this. First, add
|
|
|
|
the foreign key relation to the model::
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
# models.py
|
|
|
|
from django.contrib.auth import User
|
2012-11-14 03:46:29 +08:00
|
|
|
from django.db import models
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
class Author(models.Model):
|
|
|
|
name = models.CharField(max_length=200)
|
|
|
|
created_by = models.ForeignKey(User)
|
|
|
|
|
|
|
|
# ...
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
Create a custom :class:`~django.forms.ModelForm` in order to exclude the
|
2012-06-11 16:34:00 +08:00
|
|
|
``created_by`` field and prevent the user from editing it:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# forms.py
|
|
|
|
from django import forms
|
|
|
|
from myapp.models import Author
|
2012-11-14 03:46:29 +08:00
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
class AuthorForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Author
|
|
|
|
exclude = ('created_by',)
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
In the view, use the custom
|
|
|
|
:attr:`~django.views.generic.edit.FormMixin.form_class` and override
|
|
|
|
:meth:`~django.views.generic.edit.ModelFormMixin.form_valid()` to add the
|
|
|
|
user::
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
# views.py
|
|
|
|
from django.views.generic.edit import CreateView
|
|
|
|
from myapp.models import Author
|
|
|
|
from myapp.forms import AuthorForm
|
2012-11-14 03:46:29 +08:00
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
class AuthorCreate(CreateView):
|
|
|
|
form_class = AuthorForm
|
|
|
|
model = Author
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.created_by = self.request.user
|
|
|
|
return super(AuthorCreate, self).form_valid(form)
|
|
|
|
|
|
|
|
Note that you'll need to :ref:`decorate this
|
|
|
|
view<decorating-class-based-views>` using
|
|
|
|
:func:`~django.contrib.auth.decorators.login_required`, or
|
2013-01-01 21:12:42 +08:00
|
|
|
alternatively handle unauthorized users in the
|
|
|
|
:meth:`~django.views.generic.edit.ModelFormMixin.form_valid()`.
|
2012-07-09 15:58:24 +08:00
|
|
|
|
|
|
|
AJAX example
|
|
|
|
------------
|
|
|
|
|
|
|
|
Here is a simple example showing how you might go about implementing a form that
|
|
|
|
works for AJAX requests as well as 'normal' form POSTs::
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.views.generic.edit import CreateView
|
|
|
|
|
|
|
|
class AjaxableResponseMixin(object):
|
|
|
|
"""
|
|
|
|
Mixin to add AJAX support to a form.
|
|
|
|
Must be used with an object-based FormView (e.g. CreateView)
|
|
|
|
"""
|
|
|
|
def render_to_json_response(self, context, **response_kwargs):
|
|
|
|
data = json.dumps(context)
|
|
|
|
response_kwargs['content_type'] = 'application/json'
|
|
|
|
return HttpResponse(data, **response_kwargs)
|
|
|
|
|
|
|
|
def form_invalid(self, form):
|
|
|
|
if self.request.is_ajax():
|
|
|
|
return self.render_to_json_response(form.errors, status=400)
|
|
|
|
else:
|
|
|
|
return super(AjaxableResponseMixin, self).form_invalid(form)
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
if self.request.is_ajax():
|
|
|
|
data = {
|
|
|
|
'pk': form.instance.pk,
|
|
|
|
}
|
|
|
|
return self.render_to_json_response(data)
|
|
|
|
else:
|
|
|
|
return super(AjaxableResponseMixin, self).form_valid(form)
|
|
|
|
|
|
|
|
class AuthorCreate(AjaxableResponseMixin, CreateView):
|
|
|
|
model = Author
|