Django Select Option选择了问题 [英] Django Select Option selected issue
本文介绍了Django Select Option选择了问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试遵循一些关于在选择列表中选择的选项的stackoverflow示例,但是仍然无法正常工作。
I tried to follow some examples on stackoverflow for option selected in select list but still, I could not get it work.
这是我的代码段
<select name="topic_id" style="width:90%">
{% for t in topics %}
<option value="{{t.id}}" {% if t.id == topic_id %} selected="selected" {% endif %}>{{t.title}}{{t.id}}</option>
{% endfor %}
</select>
我从views.py中的param ['topic_id']中获取了topic_id。
I took the topic_id from param['topic_id'] in views.py.
谢谢!
推荐答案
而不是 selected = selected
只需使用 selected
。
<option value="{{t.id}}"{% if t.id == topic_id %} selected{% endif %}>{{t.title}}{{t.id}}</option>
如果它不起作用,可能有两个原因:
There can be two reason if it is not working:
- 在主题中,没有这样的主题的
id
等于topic_id
-
t.id
和topic_id
可能属于不同类型。将topic_id
转换为int
- In topics there is no such topic whose
id
is equal totopic_id
t.id
andtopic_id
may be of different types. Converttopic_id
toint
这篇关于Django Select Option选择了问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文