The choices for the form are specified in your model. Django uses these
choices for it's form-display.
It would make sense to me if Django would also provide an easy way for
you to display such 'nice' values in your interface.

The set of choices that you provide is accessible via the model; if you
model is named 'photoalbum' and your set of choices is named
'SCORE_CHOICES' then you can access it:

>>> from django.models import photoalbum
>>> print photoalbum.SCORE_CHOICES

My guess is that the best way to do it is to write a custom filter for
your display, which you pass as parameter the set of choices import as
shown above (or something like that, perhaps just the name of it will
do).

Your filter-function can then contain all the intelligence to map the
actual value to the 'nice' value from the model choices.

Other options are available too; like converting the 'tuple of tuples'
that the SCORE_CHOICES is to a dict via a list-comprehension and adding
that to your template-context; then you can use the template-syntax to
construct a dictionary-lookup in your template. Although that might not
work either, give the constraints of the Django template syntax...

Anyways I hope that I gave you some useful hints to get to the desired
result and since I will soon need to solve this problem too it was
useful for me to think about it!

Regards,

--Tim


-----Original Message-----
From: django-users@googlegroups.com
[mailto:[EMAIL PROTECTED] On Behalf Of Malcolm Tredinnick
Sent: maandag 13 februari 2006 10:00
To: Django users
Subject: Re: How to find the label for a value


PythonistL wrote:
> Luke,
> Thanks for your reply.But I am not sure I fully understand your reply.
> (I might have put my question in a not clear way).
> All that I need is to render the user's choice so that in  a template
> there is no value but the label.
>
> E.g. the new_data( taken from a form ) may be like this:
>  {'Password1': ['bb'], 'Fax': [''], 'Drop-Down Menu': ['2'], 'Login2':
> ['[EMAIL PROTECTED]'],}
>
> where Drop-Down Menu loks like this
>
>  <p><select name="Drop-Down Menu" size="1">
> >     <option value="1">Choice 1</option>
> >     <option value="2">Choice2</option>
> >   </select></p>
>
> and I need to put in a template "Choice2" not "2".

If the only place that "Choice2" appears is in your template, then the
only way to get hold of it inside Django is by not specifying a "value"
attribute on the "option" element. This is standard HTML (which was
what look was trying to say): you do not get access to the contents of
an option element _unless_ there is no value attribute specified (in
which case the default initial value for the element is its content --
see section 17.6 of the HTML spec).

So either you need to keep track of the association between the value
"2" and the text string "Choice2" in your code as well as your template
(violates 'don't repeat yourself') or you can pass back "choice2" as
part of your form (rather than "2") or you can build up the template
dynamically, inserting both "2" and "Choice2" as part of a list (so you
only store the association between the two in one place -- your code,
not the template).

Best wishes,
Malcolm

Reply via email to