开发者

django returned form option selected

开发者 https://www.devze.com 2023-01-31 07:40 出处:网络
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 sele

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 %}.

0

精彩评论

暂无评论...
验证码 换一张
取 消