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