On Saturday 11 February 2006 10:53, PythonistL wrote:

> <p><select name="Drop-Down Menu" size="1">
>     <option value="1">Choice 1</option>
>     <option value="2">Choice2</option>
>   </select></p>
>
> It is easy to find the value( here 1 and 2) but how, in Django , can
> I find the label( here Choice 1 and Choice2) ?
> E.g. when a user chooses the first option, I need to know that the
> choosen label was Choice 1

The only piece of data that is passed back is the 'value' - this is 
standard HTML and Django doesn't do any different.  You will have to 
map the values back to choices when checking the GET/POST data. 

The clever thing to do, to obey DRY, is something like this:

view module:
------------
CHOICES = {
        1: 'Choice 1',
        2: 'Choice 2',
}

view code (for creating the form):
----------------------------------
        context['choices'] = CHOICES.items()
        # render_to_response stuff etc.
        

template code:
--------------
<select name="userchoice" size="1">
        {% for choice in choices %}
             <option value="{{ choice.0 }}">{{ choice.1|escape }}</option>
        {% endfor %}
</select>

view code (for parsing input):
------------------------------
        try:
                choice = CHOICES[request.GET['userchoice']]
        except AttributeError:
                # handle invalid input


Luke

-- 
Life is complex. It has both real and imaginary components. 

Luke Plant || L.Plant.98 (at) cantab.net || http://lukeplant.me.uk/

Reply via email to