django.contrib.formtools in preview displaying only field.data by default. Its not convenient to see integer value for fields with radio buttons or select choices.
This custom tag trying to show string value from choices if available.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from django.template import Library
from django.template.defaultfilters import escape
register = Library()
@register.filter(name='field_str')
def field_str(bound_field):
field = bound_field.form.fields[bound_field.name]
if hasattr(field.widget,'choices'):
res = [x for x in field.widget.choices
if unicode(x[0]) == bound_field.data]
if len(res)>0:
return res[0][1]
return escape(bound_field.data)
|
More like this
- Form field with fixed value by roam 2 weeks, 2 days ago
- New Snippet! by Antoliny0919 3 weeks, 1 day ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 3 months, 1 week ago
- get_object_or_none by azwdevops 7 months ago
- Mask sensitive data from logger by agusmakmun 8 months, 4 weeks ago
Comments
in django 1.0 u can use get_fieldname_display()
#
Please login first before commenting.