form-preview.txt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ============
  2. Form preview
  3. ============
  4. .. module:: django.contrib.formtools.preview
  5. :synopsis: Displays an HTML form, forces a preview, then does something
  6. with the submission.
  7. Django comes with an optional "form preview" application that helps automate
  8. the following workflow:
  9. "Display an HTML form, force a preview, then do something with the submission."
  10. To force a preview of a form submission, all you have to do is write a short
  11. Python class.
  12. Overview
  13. =========
  14. Given a :class:`django.forms.Form` subclass that you define, this
  15. application takes care of the following workflow:
  16. 1. Displays the form as HTML on a Web page.
  17. 2. Validates the form data when it's submitted via POST.
  18. a. If it's valid, displays a preview page.
  19. b. If it's not valid, redisplays the form with error messages.
  20. 3. When the "confirmation" form is submitted from the preview page, calls
  21. a hook that you define -- a ``done()`` method that gets passed the valid
  22. data.
  23. The framework enforces the required preview by passing a shared-secret hash to
  24. the preview page via hidden form fields. If somebody tweaks the form parameters
  25. on the preview page, the form submission will fail the hash-comparison test.
  26. How to use ``FormPreview``
  27. ==========================
  28. 1. Point Django at the default FormPreview templates. There are two ways to
  29. do this:
  30. * Add ``'django.contrib.formtools'`` to your
  31. :setting:`INSTALLED_APPS` setting. This will work if your
  32. :setting:`TEMPLATE_LOADERS` setting includes the
  33. ``app_directories`` template loader (which is the case by
  34. default). See the :ref:`template loader docs <template-loaders>`
  35. for more.
  36. * Otherwise, determine the full filesystem path to the
  37. :file:`django/contrib/formtools/templates` directory, and add that
  38. directory to your :setting:`TEMPLATE_DIRS` setting.
  39. 2. Create a :class:`~django.contrib.formtools.preview.FormPreview` subclass that
  40. overrides the ``done()`` method::
  41. from django.contrib.formtools.preview import FormPreview
  42. from django.http import HttpResponseRedirect
  43. from myapp.models import SomeModel
  44. class SomeModelFormPreview(FormPreview):
  45. def done(self, request, cleaned_data):
  46. # Do something with the cleaned_data, then redirect
  47. # to a "success" page.
  48. return HttpResponseRedirect('/form/success')
  49. This method takes an :class:`~django.http.HttpRequest` object and a
  50. dictionary of the form data after it has been validated and cleaned.
  51. It should return an :class:`~django.http.HttpResponseRedirect` that
  52. is the end result of the form being submitted.
  53. 3. Change your URLconf to point to an instance of your
  54. :class:`~django.contrib.formtools.preview.FormPreview` subclass::
  55. from myapp.preview import SomeModelFormPreview
  56. from myapp.forms import SomeModelForm
  57. from django import forms
  58. ...and add the following line to the appropriate model in your URLconf::
  59. url(r'^post/$', SomeModelFormPreview(SomeModelForm)),
  60. where ``SomeModelForm`` is a Form or ModelForm class for the model.
  61. 4. Run the Django server and visit :file:`/post/` in your browser.
  62. ``FormPreview`` classes
  63. =======================
  64. .. class:: FormPreview
  65. A :class:`~django.contrib.formtools.preview.FormPreview` class is a simple Python class
  66. that represents the preview workflow.
  67. :class:`~django.contrib.formtools.preview.FormPreview` classes must subclass
  68. ``django.contrib.formtools.preview.FormPreview`` and override the ``done()``
  69. method. They can live anywhere in your codebase.
  70. ``FormPreview`` templates
  71. =========================
  72. .. attribute:: FormPreview.form_template
  73. .. attribute:: FormPreview.preview_template
  74. By default, the form is rendered via the template :file:`formtools/form.html`,
  75. and the preview page is rendered via the template :file:`formtools/preview.html`.
  76. These values can be overridden for a particular form preview by setting
  77. :attr:`~django.contrib.formtools.preview.FormPreview.preview_template` and
  78. :attr:`~django.contrib.formtools.preview.FormPreview.form_template` attributes on the
  79. FormPreview subclass. See :file:`django/contrib/formtools/templates` for the
  80. default templates.
  81. Advanced ``FormPreview`` methods
  82. ================================
  83. .. method:: FormPreview.process_preview()
  84. Given a validated form, performs any extra processing before displaying the
  85. preview page, and saves any extra data in context.
  86. By default, this method is empty. It is called after the form is validated,
  87. but before the context is modified with hash information and rendered.