@register.inclusion_tag("form_tools/render_field.html")
def render_field(field,attributes=''):
""" render a field with its errors, optionally passing in
attributes eg.:
{% render_field form.name "cols=40,rows=5,class=text,tabindex=2" %}
this is equivalent to
<p>{{form.name.errors}}</p>
{{ form.name }}
but will also add the custom attributes
"""
return {'errors':field.errors,'widget':make_widget(field,attributes)}
def make_widget(field,attributes):
attr = {}
if attributes:
attrs = attributes.split(",")
if attrs:
for at in attrs:
key,value = at.split("=")
attr[key] = value
return field.as_widget(attrs=attr)
## render_field.html
{% if errors %}<p>{{ errors }}</p>{% endif %}
{{ widget }}
Comments
This is a a bit simpler:
Usage:
#