Quantcast
Channel: Django templates: verbose version of a choice - Stack Overflow
Viewing all articles
Browse latest Browse all 12

Django templates: verbose version of a choice

$
0
0

I have a model:

from django.db import modelsCHOICES = (    ('s', 'Glorious spam'),    ('e', 'Fabulous eggs'),)class MealOrder(models.Model):    meal = models.CharField(max_length=8, choices=CHOICES)

I have a form:

from django.forms import ModelFormclass MealOrderForm(ModelForm):    class Meta:        model = MealOrder

And I want to use formtools.preview. The default template prints the short version of the choice ('e' instead of 'Fabulous eggs'), becuase it uses

{% for field in form %}<tr><th>{{ field.label }}:</th><td>{{ field.data }}</td></tr>{% endfor %}.

I'd like a template as general as the mentioned, but printing 'Fabulous eggs' instead.

[as I had doubts where's the real question, I bolded it for all of us :)]

I know how to get the verbose version of a choice in a way that is itself ugly:

{{ form.meal.field.choices.1.1 }}

The real pain is I need to get the selected choice, and the only way coming to my mind is iterating through choices and checking {% ifequals currentChoice.0 choiceField.data %}, which is even uglier.

Can it be done easily? Or it needs some template-tag programming? Shouldn't that be available in django already?


Viewing all articles
Browse latest Browse all 12

Latest Images

Trending Articles





Latest Images