Login

TemplatedForm

Author:
alexkoval
Posted:
March 21, 2007
Language:
Python
Version:
Pre .96
Score:
3 (after 3 ratings)

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 usage guide: 1. Create file in your project yourproject/utils/newforms.py and place Class TemplatedForm there 2. Create newforms subdirectory in templates dir, and post 2 templates there (form.html, field.html) from the documentation code for TemplatedForm class 3. Inherit from TemplatedForm in your form class to use this form.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from django import newforms as forms
from django.newforms.forms import BoundField
from django.template import Context, loader

class TemplatedForm(forms.Form):
    '''
    template newforms/form.html:
    % for field in bound_fields %}
    {% include "newforms/field.html" %}
    {% endfor %}


    template newforms/field.html:
    <tr{% if field.errors %} class="errors" {% endif%}>
    <th>
    <label for="id_{{ field.name }}">{{ field.label }}{% if field.field.required %}<span class="required">*</span>{% endif %}:</label>
    </th>
    <td>
    {{ field }}
    {% if field.errors %}{{ field.errors }}{% endif %}
    {% if field.help_text %}
    <p class="helptext">({{ field.help_text }})</p>
    {% endif %}
    </td>
    </tr>
    '''
    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()

# example usage in view 
class PersonalInfoForm(TemplatedForm):
    school_or_institution = forms.CharField(max_length=100,required=False)
    first_name = forms.CharField(max_length=100,required=False)
    last_name = forms.CharField(max_length=100,required=False)
    email = forms.EmailField()
    personal_website_url = forms.CharField(max_length=100,required=False)
    button = forms.CharField(required=False,widget=forms.HiddenInput)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

nnachefski (on June 2, 2011):

It worked beautifully! I used it to generate a custom ModelForm (you have to change the base class accordingly). Thank you sir.

#

Please login first before commenting.