== Subclassing newforms Form.form to rendering form via django template == Newforms are made to make any kind of customizations easy. Sometimes standard methods of rendering HTML/XML/other content of the django.newforms is not enough to completely satisfy all web design needs so you may want to present forms in own templates. Using templates to render output as HTML or XML is straighforward, and in many cases easier then using standard approach. == Step by step guide == 1. Create file in your project: yourproject/utils/newforms.py: {{{ #!python from django import newforms as forms from django.newforms.forms import BoundField from django.template import Context, loader class TemplatedForm(forms.Form): def output_via_template(self): "Helper function for fieldsting fields data from form." bound_fields = [BoundField(self, field, name) for name, field \ in self.fields.items()] c = Context(dict(form = self, bound_fields = bound_fields)) t = loader.get_template('newforms/form.html') return t.render(c) def __str__(self): return self.output_via_template() }}} 2. Create HTML templates, here is very simple template form form and field: file: templates/newforms/form.html {{{ {% for field in bound_fields %} {% include "newforms/field.html" %} {% endfor %} }}} file: templates/newforms/field.html {{{
({{ field.help_text }})
{% endif %}