Login

Preview tag for fields with choices

Author:
kmmbvnr
Posted:
April 11, 2008
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

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

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 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, 6 months ago

Comments

slav0nic (on November 25, 2008):

in django 1.0 u can use get_fieldname_display()

#

Please login first before commenting.