Django: How to access the display value of a ChoiceField in template given the actual value and the choices? Django: How to access the display value of a ChoiceField in template given the actual value and the choices? django django

Django: How to access the display value of a ChoiceField in template given the actual value and the choices?


I doubt that it can be done without custom template tag or filter.Custom template filter could look:

@register.filterdef selected_choice(form, field_name):    return dict(form.fields[field_name].choices)[form.data[field_name]]


Use the get_FOO_display property.

** Edit **

Oups! Quick editing this answer, after reading the comments below.

bound_form['field'].value()

Should work according to this changeset


Check this link - https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.get_FOO_display

You can use this function which will return the display name - ObjectName.get_FieldName_display()

Replace ObjectName with your class name and FieldName with the field of which you need to fetch the display name of.