2008-08-23 22:25:40 +00:00
|
|
|
============
|
|
|
|
Form preview
|
|
|
|
============
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
.. module:: django.contrib.formtools.preview
|
2008-08-23 22:25:40 +00:00
|
|
|
:synopsis: Displays an HTML form, forces a preview, then does something
|
|
|
|
with the submission.
|
|
|
|
|
|
|
|
Django comes with an optional "form preview" application that helps automate
|
|
|
|
the following workflow:
|
|
|
|
|
|
|
|
"Display an HTML form, force a preview, then do something with the submission."
|
|
|
|
|
|
|
|
To force a preview of a form submission, all you have to do is write a short
|
|
|
|
Python class.
|
|
|
|
|
|
|
|
Overview
|
|
|
|
=========
|
|
|
|
|
|
|
|
Given a :class:`django.forms.Form` subclass that you define, this
|
|
|
|
application takes care of the following workflow:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
1. Displays the form as HTML on a Web page.
|
|
|
|
2. Validates the form data when it's submitted via POST.
|
|
|
|
a. If it's valid, displays a preview page.
|
|
|
|
b. If it's not valid, redisplays the form with error messages.
|
|
|
|
3. When the "confirmation" form is submitted from the preview page, calls
|
2013-01-01 13:12:42 +00:00
|
|
|
a hook that you define -- a ``done()`` method that gets passed the valid
|
|
|
|
data.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
The framework enforces the required preview by passing a shared-secret hash to
|
|
|
|
the preview page via hidden form fields. If somebody tweaks the form parameters
|
|
|
|
on the preview page, the form submission will fail the hash-comparison test.
|
|
|
|
|
|
|
|
How to use ``FormPreview``
|
|
|
|
==========================
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
1. Point Django at the default FormPreview templates. There are two ways to
|
|
|
|
do this:
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Add ``'django.contrib.formtools'`` to your
|
|
|
|
:setting:`INSTALLED_APPS` setting. This will work if your
|
|
|
|
:setting:`TEMPLATE_LOADERS` setting includes the
|
|
|
|
``app_directories`` template loader (which is the case by
|
|
|
|
default). See the :ref:`template loader docs <template-loaders>`
|
|
|
|
for more.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Otherwise, determine the full filesystem path to the
|
|
|
|
:file:`django/contrib/formtools/templates` directory, and add that
|
|
|
|
directory to your :setting:`TEMPLATE_DIRS` setting.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
2. Create a :class:`~django.contrib.formtools.preview.FormPreview` subclass that
|
2013-01-01 13:12:42 +00:00
|
|
|
overrides the ``done()`` method::
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
from django.contrib.formtools.preview import FormPreview
|
2013-05-19 09:15:35 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2011-10-14 00:12:01 +00:00
|
|
|
from myapp.models import SomeModel
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
class SomeModelFormPreview(FormPreview):
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
def done(self, request, cleaned_data):
|
|
|
|
# Do something with the cleaned_data, then redirect
|
|
|
|
# to a "success" page.
|
|
|
|
return HttpResponseRedirect('/form/success')
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
This method takes an :class:`~django.http.HttpRequest` object and a
|
|
|
|
dictionary of the form data after it has been validated and cleaned.
|
|
|
|
It should return an :class:`~django.http.HttpResponseRedirect` that
|
|
|
|
is the end result of the form being submitted.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
3. Change your URLconf to point to an instance of your
|
|
|
|
:class:`~django.contrib.formtools.preview.FormPreview` subclass::
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
from myapp.preview import SomeModelFormPreview
|
|
|
|
from myapp.forms import SomeModelForm
|
|
|
|
from django import forms
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
...and add the following line to the appropriate model in your URLconf::
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
(r'^post/$', SomeModelFormPreview(SomeModelForm)),
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
where ``SomeModelForm`` is a Form or ModelForm class for the model.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
4. Run the Django server and visit :file:`/post/` in your browser.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
``FormPreview`` classes
|
|
|
|
=======================
|
|
|
|
|
|
|
|
.. class:: FormPreview
|
|
|
|
|
2011-02-16 01:56:53 +00:00
|
|
|
A :class:`~django.contrib.formtools.preview.FormPreview` class is a simple Python class
|
2008-08-23 22:25:40 +00:00
|
|
|
that represents the preview workflow.
|
2011-02-16 01:56:53 +00:00
|
|
|
:class:`~django.contrib.formtools.preview.FormPreview` classes must subclass
|
2013-01-01 13:12:42 +00:00
|
|
|
``django.contrib.formtools.preview.FormPreview`` and override the ``done()``
|
|
|
|
method. They can live anywhere in your codebase.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
``FormPreview`` templates
|
|
|
|
=========================
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
.. attribute:: FormPreview.form_template
|
|
|
|
.. attribute:: FormPreview.preview_template
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
By default, the form is rendered via the template :file:`formtools/form.html`,
|
2008-08-31 07:54:31 +00:00
|
|
|
and the preview page is rendered via the template :file:`formtools/preview.html`.
|
2008-08-23 22:25:40 +00:00
|
|
|
These values can be overridden for a particular form preview by setting
|
2011-02-16 01:56:53 +00:00
|
|
|
:attr:`~django.contrib.formtools.preview.FormPreview.preview_template` and
|
|
|
|
:attr:`~django.contrib.formtools.preview.FormPreview.form_template` attributes on the
|
2008-08-23 22:25:40 +00:00
|
|
|
FormPreview subclass. See :file:`django/contrib/formtools/templates` for the
|
|
|
|
default templates.
|
2010-02-22 05:00:36 +00:00
|
|
|
|
|
|
|
Advanced ``FormPreview`` methods
|
|
|
|
================================
|
|
|
|
|
|
|
|
.. method:: FormPreview.process_preview
|
|
|
|
|
|
|
|
Given a validated form, performs any extra processing before displaying the
|
|
|
|
preview page, and saves any extra data in context.
|
|
|
|
|
|
|
|
By default, this method is empty. It is called after the form is validated,
|
|
|
|
but before the context is modified with hash information and rendered.
|