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.
Given a django.forms.Form
subclass that you define, this
application takes care of the following workflow:
done()
method that gets passed the valid
data.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.
FormPreview
¶Point Django at the default FormPreview templates. There are two ways to do this:
'django.contrib.formtools'
to your
INSTALLED_APPS
setting. This will work if your
TEMPLATE_LOADERS
setting includes the
app_directories
template loader (which is the case by
default). See the template loader docs
for more.django/contrib/formtools/templates
directory, and add that
directory to your TEMPLATE_DIRS
setting.Create a FormPreview
subclass that
overrides the done()
method:
from django.contrib.formtools.preview import FormPreview
from django.http import HttpResponseRedirect
from myapp.models import SomeModel
class SomeModelFormPreview(FormPreview):
def done(self, request, cleaned_data):
# Do something with the cleaned_data, then redirect
# to a "success" page.
return HttpResponseRedirect('/form/success')
This method takes an HttpRequest
object and a
dictionary of the form data after it has been validated and cleaned.
It should return an HttpResponseRedirect
that
is the end result of the form being submitted.
Change your URLconf to point to an instance of your
FormPreview
subclass:
from myapp.preview import SomeModelFormPreview
from myapp.forms import SomeModelForm
from django import forms
...and add the following line to the appropriate model in your URLconf:
(r'^post/$', SomeModelFormPreview(SomeModelForm)),
where SomeModelForm
is a Form or ModelForm class for the model.
Run the Django server and visit /post/
in your browser.
FormPreview
classes¶A FormPreview
class is a simple Python class
that represents the preview workflow.
FormPreview
classes must subclass
django.contrib.formtools.preview.FormPreview
and override the done()
method. They can live anywhere in your codebase.
FormPreview
templates¶FormPreview.
form_template
¶FormPreview.
preview_template
¶By default, the form is rendered via the template formtools/form.html
,
and the preview page is rendered via the template formtools/preview.html
.
These values can be overridden for a particular form preview by setting
preview_template
and
form_template
attributes on the
FormPreview subclass. See django/contrib/formtools/templates
for the
default templates.
FormPreview
methods¶FormPreview.
process_preview
()[source]¶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.
Feb 24, 2017