From 7617e73c8cfe99601645ce487781ecc86f8c6536 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 15 Sep 2008 09:30:05 +0000 Subject: [PATCH] Added documentation on creating reusable form templates; thanks for the suggestion, oggie rob git-svn-id: http://code.djangoproject.com/svn/django/trunk@9033 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- docs/topics/forms/index.txt | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/docs/topics/forms/index.txt b/docs/topics/forms/index.txt index cf6b933ee4e..8ac950e5891 100644 --- a/docs/topics/forms/index.txt +++ b/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:: + +
+ {% include "form_snippet.html" %} +

+
+ + # In form_snippet.html: + + {% for field in form %} +
+ {{ field.errors }} + {{ field.label_tag }}: {{ field }} +
+ {% 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:: + +
+ {% with comment_form as form %} + {% include "form_snippet.html" %} + {% endwith %} +

+
+ Further topics ==============