views:

404

answers:

1

Hi all, I am trying to create a drop down list box with the selected value equal to a value passed from the template values, but with no success. Can anyone take a look and show me what I am doing wrong.

     <select name="movie">
       {% for movie in movies %}
    {% ifequal movie.id selected_movie.id %}
     <option value="{{movie.key}}" selected="true">Movie {{movie.id}}: {{movie.name}}</option>
     {% endifequal %}
     {% ifnotequal movie.id selected_movie.id %}
     <option value="{{movie.key}}">Movie {{movie.id}}: {{movie.name}}</option>
     {% endifnotequal %}
       {% endfor %}
     </select>

In this example, movies and selected_movie are passed from the template values. Please advice!

+3  A: 

Your code works for me with django 1.0.2 and firefox 3.5.

You can use {% else %} instead of {% ifnotequal %} and set selected="selected". Hope it helps.

<select name="movie">
    {% for movie in movies %}
        {% ifequal movie.id selected_movie.id %}
            <option value="{{movie.key}}" selected="selected">Movie {{movie.id}}: {{movie.name}}</option>
        {% else %}
            <option value="{{movie.key}}">Movie {{movie.id}}: {{movie.name}}</option>
        {% endifnotequal %}
    {% endfor %}
</select>
Guillem Gelabert
yeah, thanks Guillem, it works for me as well.
sfa