Login

Stacked/Grouped Forms 2 - easy rendering forms

Author:
diverman
Posted:
November 9, 2009
Language:
Python
Version:
1.1
Tags:
field stacked groups fieldset forms object descriptor fieldsets fields template form templates
Score:
1 (after 2 ratings)

This snippet was inspired by 1783. It allows simply create groups of fields for template rendering.

 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
from django.forms.forms import BoundField

class FieldStack(object):
    """ EXAMPLE:

    class MyForm(forms.Form):
        first_name = forms.CharField()
        last_name = forms.CharField()
        website = forms.CharField()
        twitter = forms.CharField()
        facebook = forms.CharField()

        personal_info = FieldStack('first_name', 'last_name')
        contact_info = FieldStack('website', 'twitter', 'facebook')
    
    <ul>{% for field in form.personal_info %}
        <li>{{field.name}}: {{ field }}{{field.errors}}</li>
    {% endfor %}</ul>
    
    <table>{% for field in form.contact_info %}
        <tr><td>{{field.name}}</td><td>{{ field }}</td><td>{{field.errors}}</td></tr>
    {% endfor %}</table>
    """

    def __init__(self, *args):
        self.fields = args
        
    def __get__(self, form, objtype=None):
        for field in self.fields:
            yield BoundField(form, form.fields[field], field)

More like this

  1. FieldStack - easy form template rendering by diverman 4 years, 11 months ago
  2. Stacked/Grouped Forms by flashingpumpkin 5 years, 6 months ago
  3. Form field sets by gonz 5 years, 3 months ago
  4. FieldsetForm by Ciantic 8 years ago
  5. BetterForm with fieldsets and row_attrs by carljm 6 years, 2 months ago

Comments

Please login first before commenting.