Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added documentation on creating reusable form templates; thanks for t…

…he suggestion, oggie rob

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9033 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7617e73c8cfe99601645ce487781ecc86f8c6536 1 parent 6fcdcbd
Simon Willison authored
Showing with 31 additions and 0 deletions.
  1. +31 −0 docs/topics/forms/index.txt
View
31 docs/topics/forms/index.txt
@@ -290,6 +290,37 @@ templates:
corresponding to this field. You can customize the presentation of
the errors with a ``{% for error in field.errors %}`` loop.
+Reusable form templates
+-----------------------
+
+If your site uses the same rendering logic for forms in multiple places you
+can create a template that contains just the form loop and use the
+:ttag:`include` tag to reuse it in your other templates::
+
+ <form action="/contact/" method="POST">
+ {% include "form_snippet.html" %}
+ <p><input type="submit" value="Send message"></p>
+ </form>
+
+ # In form_snippet.html:
+
+ {% for field in form %}
+ <div class="fieldWrapper">
+ {{ field.errors }}
+ {{ field.label_tag }}: {{ field }}
+ </div>
+ {% endfor %}
+
+If the form object passed to a template has a different name within the
+context you can alias it using the :ttag:`with` tag::
+
+ <form action="/comments/add/" method="POST">
+ {% with comment_form as form %}
+ {% include "form_snippet.html" %}
+ {% endwith %}
+ <p><input type="submit" value="Submit comment"></p>
+ </form>
+
Further topics
==============
Please sign in to comment.
Something went wrong with that request. Please try again.