2016-01-03 11:56:22 +01:00
|
|
|
====================================
|
2012-06-11 10:34:00 +02: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 14:12:42 +01: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 10:34:00 +02:00
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
Basic forms
|
2016-01-03 11:56:22 +01:00
|
|
|
===========
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2019-06-17 16:54:55 +02:00
|
|
|
Given a contact form:
|
2014-08-18 16:30:44 +02:00
|
|
|
|
2018-09-10 19:00:34 +02:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: forms.py
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-08-18 16:30:44 +02:00
|
|
|
The view can be constructed using a ``FormView``:
|
|
|
|
|
2018-09-10 19:00:34 +02:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: views.py
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
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()
|
2017-01-22 07:57:14 +01:00
|
|
|
return super().form_valid(form)
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
|
|
|
* FormView inherits
|
|
|
|
:class:`~django.views.generic.base.TemplateResponseMixin` so
|
|
|
|
:attr:`~django.views.generic.base.TemplateResponseMixin.template_name`
|
2013-01-01 14:12:42 +01:00
|
|
|
can be used here.
|
2012-06-11 10:34:00 +02:00
|
|
|
* The default implementation for
|
2013-01-01 14:12:42 +01:00
|
|
|
:meth:`~django.views.generic.edit.FormMixin.form_valid` simply
|
|
|
|
redirects to the :attr:`~django.views.generic.edit.FormMixin.success_url`.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
Model forms
|
2016-01-03 11:56:22 +01:00
|
|
|
===========
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
Generic views really shine when working with models. These generic
|
2013-01-01 14:12:42 +01: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 10:34:00 +02:00
|
|
|
special requirements; see below for examples.
|
|
|
|
|
2013-01-01 14:12:42 +01:00
|
|
|
You don't even need to provide a ``success_url`` for
|
2012-06-11 10:34:00 +02:00
|
|
|
:class:`~django.views.generic.edit.CreateView` or
|
|
|
|
:class:`~django.views.generic.edit.UpdateView` - they will use
|
2013-01-01 14:12:42 +01:00
|
|
|
:meth:`~django.db.models.Model.get_absolute_url()` on the model object if available.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2013-01-01 14:12:42 +01:00
|
|
|
If you want to use a custom :class:`~django.forms.ModelForm` (for instance to
|
2019-06-17 16:54:55 +02:00
|
|
|
add extra validation), set
|
2012-06-11 10:34:00 +02: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 14:12:42 +01:00
|
|
|
even though the :attr:`~django.views.generic.edit.FormMixin.form_class` may
|
|
|
|
be a :class:`~django.forms.ModelForm`.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2013-01-01 14:12:42 +01:00
|
|
|
First we need to add :meth:`~django.db.models.Model.get_absolute_url()` to our
|
|
|
|
``Author`` class:
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2018-09-10 19:00:34 +02:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: models.py
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2012-11-13 20:46:29 +01:00
|
|
|
from django.db import models
|
2018-05-12 19:37:42 +02:00
|
|
|
from django.urls import reverse
|
2012-06-11 10:34:00 +02: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
|
2014-08-18 16:30:44 +02:00
|
|
|
here; we don't have to write any logic ourselves:
|
|
|
|
|
2018-09-10 19:00:34 +02:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: views.py
|
2012-11-13 20:46:29 +01:00
|
|
|
|
2015-12-30 16:51:16 +01:00
|
|
|
from django.urls import reverse_lazy
|
2018-05-12 19:37:42 +02:00
|
|
|
from django.views.generic.edit import CreateView, DeleteView, UpdateView
|
2012-06-11 10:34:00 +02:00
|
|
|
from myapp.models import Author
|
|
|
|
|
|
|
|
class AuthorCreate(CreateView):
|
|
|
|
model = Author
|
2013-02-21 22:56:55 +01:00
|
|
|
fields = ['name']
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
class AuthorUpdate(UpdateView):
|
|
|
|
model = Author
|
2013-02-21 22:56:55 +01:00
|
|
|
fields = ['name']
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
class AuthorDelete(DeleteView):
|
|
|
|
model = Author
|
|
|
|
success_url = reverse_lazy('author-list')
|
|
|
|
|
|
|
|
.. note::
|
2019-06-17 16:54:55 +02:00
|
|
|
We have to use :func:`~django.urls.reverse_lazy` instead of
|
|
|
|
``reverse()``, as the urls are not loaded when the file is imported.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2014-03-22 01:44:34 +01:00
|
|
|
The ``fields`` attribute works the same way as the ``fields`` attribute on the
|
|
|
|
inner ``Meta`` class on :class:`~django.forms.ModelForm`. Unless you define the
|
|
|
|
form class in another way, the attribute is required and the view will raise
|
|
|
|
an :exc:`~django.core.exceptions.ImproperlyConfigured` exception if it's not.
|
2013-02-21 22:56:55 +01:00
|
|
|
|
2014-11-15 12:17:55 +01:00
|
|
|
If you specify both the :attr:`~django.views.generic.edit.ModelFormMixin.fields`
|
|
|
|
and :attr:`~django.views.generic.edit.FormMixin.form_class` attributes, an
|
|
|
|
:exc:`~django.core.exceptions.ImproperlyConfigured` exception will be raised.
|
|
|
|
|
2014-08-18 16:30:44 +02:00
|
|
|
Finally, we hook these new views into the URLconf:
|
|
|
|
|
2018-09-10 19:00:34 +02:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: urls.py
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2016-10-20 19:29:04 +02:00
|
|
|
from django.urls import path
|
2018-05-12 19:37:42 +02:00
|
|
|
from myapp.views import AuthorCreate, AuthorDelete, AuthorUpdate
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2014-04-02 02:46:34 +02:00
|
|
|
urlpatterns = [
|
2012-06-11 10:34:00 +02:00
|
|
|
# ...
|
2016-10-20 19:29:04 +02:00
|
|
|
path('author/add/', AuthorCreate.as_view(), name='author-add'),
|
|
|
|
path('author/<int:pk>/', AuthorUpdate.as_view(), name='author-update'),
|
|
|
|
path('author/<int:pk>/delete/', AuthorDelete.as_view(), name='author-delete'),
|
2014-04-02 02:46:34 +02:00
|
|
|
]
|
2012-11-13 20:46:29 +01:00
|
|
|
|
2012-06-11 10:34:00 +02:00
|
|
|
.. note::
|
|
|
|
|
2013-01-01 14:12:42 +01:00
|
|
|
These views inherit
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
|
|
|
|
which uses
|
|
|
|
:attr:`~django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix`
|
2012-06-11 10:34:00 +02: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 14:12:42 +01: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 10:34:00 +02:00
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
Models and ``request.user``
|
|
|
|
===========================
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
To track the user that created an object using a :class:`CreateView`,
|
2013-01-01 14:12:42 +01:00
|
|
|
you can use a custom :class:`~django.forms.ModelForm` to do this. First, add
|
2014-08-18 16:30:44 +02:00
|
|
|
the foreign key relation to the model:
|
|
|
|
|
2018-09-10 19:00:34 +02:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: models.py
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2014-05-05 12:41:01 +02:00
|
|
|
from django.contrib.auth.models import User
|
2012-11-13 20:46:29 +01:00
|
|
|
from django.db import models
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
class Author(models.Model):
|
|
|
|
name = models.CharField(max_length=200)
|
2015-07-22 16:43:21 +02:00
|
|
|
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
# ...
|
|
|
|
|
2013-06-22 23:05:22 +02:00
|
|
|
In the view, ensure that you don't include ``created_by`` in the list of fields
|
|
|
|
to edit, and override
|
2014-08-18 16:30:44 +02:00
|
|
|
:meth:`~django.views.generic.edit.ModelFormMixin.form_valid()` to add the user:
|
|
|
|
|
2018-09-10 19:00:34 +02:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: views.py
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2019-02-01 00:01:53 +01:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2012-06-11 10:34:00 +02:00
|
|
|
from django.views.generic.edit import CreateView
|
|
|
|
from myapp.models import Author
|
2012-11-13 20:46:29 +01:00
|
|
|
|
2019-02-01 00:01:53 +01:00
|
|
|
class AuthorCreate(LoginRequiredMixin, CreateView):
|
2012-06-11 10:34:00 +02:00
|
|
|
model = Author
|
2013-02-21 22:56:55 +01:00
|
|
|
fields = ['name']
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.created_by = self.request.user
|
2017-01-22 07:57:14 +01:00
|
|
|
return super().form_valid(form)
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2019-02-01 00:01:53 +01:00
|
|
|
:class:`~django.contrib.auth.mixins.LoginRequiredMixin` prevents users who
|
|
|
|
aren't logged in from accessing the form. If you omit that, you'll need to
|
|
|
|
handle unauthorized users in :meth:`~.ModelFormMixin.form_valid()`.
|
2012-07-09 09:58:24 +02:00
|
|
|
|
|
|
|
AJAX example
|
2016-01-03 11:56:22 +01:00
|
|
|
============
|
2012-07-09 09:58:24 +02:00
|
|
|
|
2019-06-17 16:54:55 +02:00
|
|
|
Here is an example showing how you might go about implementing a form that
|
2012-07-09 09:58:24 +02:00
|
|
|
works for AJAX requests as well as 'normal' form POSTs::
|
|
|
|
|
2014-07-29 13:45:16 +02:00
|
|
|
from django.http import JsonResponse
|
2012-07-09 09:58:24 +02:00
|
|
|
from django.views.generic.edit import CreateView
|
2013-05-18 14:00:52 +02:00
|
|
|
from myapp.models import Author
|
2012-07-09 09:58:24 +02:00
|
|
|
|
2017-06-26 16:30:31 +02:00
|
|
|
class AjaxableResponseMixin:
|
2012-07-09 09:58:24 +02:00
|
|
|
"""
|
|
|
|
Mixin to add AJAX support to a form.
|
|
|
|
Must be used with an object-based FormView (e.g. CreateView)
|
|
|
|
"""
|
|
|
|
def form_invalid(self, form):
|
2017-01-22 07:57:14 +01:00
|
|
|
response = super().form_invalid(form)
|
2012-07-09 09:58:24 +02:00
|
|
|
if self.request.is_ajax():
|
2014-07-29 13:45:16 +02:00
|
|
|
return JsonResponse(form.errors, status=400)
|
2012-07-09 09:58:24 +02:00
|
|
|
else:
|
2013-04-22 19:04:28 +02:00
|
|
|
return response
|
2012-07-09 09:58:24 +02:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
2013-04-22 19:04:28 +02:00
|
|
|
# We make sure to call the parent's form_valid() method because
|
|
|
|
# it might do some processing (in the case of CreateView, it will
|
|
|
|
# call form.save() for example).
|
2017-01-22 07:57:14 +01:00
|
|
|
response = super().form_valid(form)
|
2012-07-09 09:58:24 +02:00
|
|
|
if self.request.is_ajax():
|
|
|
|
data = {
|
2013-04-22 19:04:28 +02:00
|
|
|
'pk': self.object.pk,
|
2012-07-09 09:58:24 +02:00
|
|
|
}
|
2014-07-29 13:45:16 +02:00
|
|
|
return JsonResponse(data)
|
2012-07-09 09:58:24 +02:00
|
|
|
else:
|
2013-04-22 19:04:28 +02:00
|
|
|
return response
|
2012-07-09 09:58:24 +02:00
|
|
|
|
|
|
|
class AuthorCreate(AjaxableResponseMixin, CreateView):
|
|
|
|
model = Author
|
2013-06-22 23:05:22 +02:00
|
|
|
fields = ['name']
|