I have a drop down menu from which people can select an area and submit. On being directed to the results page I would like the chosen area to be displayed in the form. Convention is that you use selected as an attibute in the option tag.
Using django,
area = area 开发者_如何学运维queryset to populate the drop down list. q = chosen for area. Both can be renderd fine in the template seperately. However when I try,
<form action="/results/" method="GET">
<select name="q" id="id_area">
{% if a %}
{% for area in a %}
<option value="{{ area }}" {% if area == q %} selected {% endif %}>{{ area }}</option>
{% endfor %}
{% endif %}
</select>
<p><input class="send_button" type="submit" value="Search" /></p>
</form>
It doesn't give the "selected" attibute.
Also tried,
{% for area in a %}
<option value="{{ area }}" {% ifequal area q %} selected {% endif %}>{{ area }}</option>
{% endfor %}
Any help would be much appreciated. Thanks
I'm quite sure the django template equality operator doesn't support var == var
checking. I'd suggest adding a 'selected' property to area so you can say something like {% if area.selected %}
.
精彩评论