Django templates: verbose version of a choice?

In Django templates you can use the get_FOO_display() method, that will return the readable alias for the field.

In Django templates you can use the "get_FOO_display()" method, that will return the readable alias for the field. Note: in case the standard FormPreview templates are not using it, then you can always provide your own templates for that form, which will contain something like {{ form. Get_meal_display }}.

1 yes, I know. It's not as general (universal), though - unless you know a way to iterate in a template over all get_FOO_display methods of a model object :) I'm a bit too lazy for writing non-generic templates ;) Moreover, the docs say it's a model instance's method. Therefore it'd have to be a model form bound to an existing object which is not the case and also not general.

– Artur Gajowy Jul 9 '09 at 22:40 I am alway forgetting this one – Sevenearths Sep 23 at 9:55.

Basing on Noah's reply, here's a version immune to fields without choices: #annoyances/templatetags/data_verbose. Py from django import template register = template.Library() @register. Filter def data_verbose(boundField): """ Returns field's data or it's verbose version for a field with choices defined.

Usage:: {% load data_verbose %} {{form. Some_field|data_verbose}} """ data = boundField. Data field = boundField.

Field return hasattr(field, 'choices') and dict(field. Choices). Get(data,'') or data I'm not sure wether it's ok to use a filter for such purpose.

If anybody has a better solution, I'll be glad to see it :) Thank you Noah!

1 for mentioning your path #annoyances/templatetags/... LOL ... I use get_FOO_display(), which is mentioned on the bottom of the form docs. – Frank Feb 15 at 15:04.

I don't think there's any built-in way to do that. A filter might do the trick, though: @register. Filter(name='display') def display_value(bf): """Returns the display value of a BoundField""" return dict(bf.field.

Choices). Get(bf. Data, '') Then you can do: {% for field in form %} {{ field.

Label }}: {{ field. Data|display }} {% endfor %}.

The best solution for your problem is to use helper functions. If the choices are stored in the variable CHOICES and the model field storing the selected choice is 'choices' then you can directly use {{ x. Get_choices_display }} in your template.

Here, x is the model instance. Hope it helps.

We can extend the filter solution by Noah to be more universal in dealing with data and field types: {% for item in query %} {% for field in fields %} {{item|human_readable:field}} {% endfor %} {% endfor %} Here's the code: #app_name/templatetags/custom_tags. Py def human_readable(value, arg): if hasattr(value, 'get_' + str(arg) + '_display'): return getattr(value, 'get_%s_display' % arg)() elif hasattr(value, str(arg)): if callable(getattr(value, str(arg))): return getattr(value, arg)() else: return getattr(value, arg) else: try: return valuearg except KeyError: return settings. TEMPLATE_STRING_IF_INVALID register.

Filter('human_readable', human_readable).

Seems quite universal :) Can't tell for sure, because I haven't done too much Python or Django since that time. It's pretty sad, though, that it still needs a 3rd party (not included in Django) filter (otherwise you'd tell us, Ivan, wouldn't you? ;))... – Artur Gajowy Sep 30 at 21:38 @ArturGajowy Yes, as of today there is no such default feature in Django.

I have proposed it, who knows, maybe it will be approved. – Ivan Kharlamov Oct 3 at 8:18.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions