Python 比较django模板中的值
我的view.py中有以下数组Python 比较django模板中的值,python,django,django-templates,Python,Django,Django Templates,我的view.py中有以下数组 chapter = 5 number_of_chapters = 5 selected_chapters = list() for index in range(1, number_of_chapters+1): selected_chapters.append(index) return render(request, 'verses.html', {'chapter': chapter, 'selected_chapters': selected_ch
chapter = 5
number_of_chapters = 5
selected_chapters = list()
for index in range(1, number_of_chapters+1):
selected_chapters.append(index)
return render(request, 'verses.html', {'chapter': chapter, 'selected_chapters': selected_chapters})
但是,当我尝试将此列表中的条目与章节
进行比较时,它永远不会返回true
模板
<select name="c" id="chapter" value="{{ chapter }}" onchange="bible_search(true);">
<option value="1">Chapter</option>
{% for c in selected_chapters %}
<option {% if c == chapter %} selected {% endif %} value="{{ c }}">{{ c }}</option>
{% endfor %}
</select>
章
{所选章节中c的百分比%}
{{c}}
{%endfor%}
我想知道我是否需要将字符串转换为int或其他什么 似乎您还没有发布完整的代码,因为这对我来说很好。使用您提供的代码,当我导航到从该视图呈现的页面时,我看到一个
select
,其值为5
。我想这就是你想要的?这对我来说意味着视图或模板中的其他地方存在一些冲突
在这里,HTMLselect
元素没有value
属性,因此应该删除该属性。其次,您有两个具有相同值的option
元素。这是在浏览器中呈现的HTML
<select name="c" id="chapter" value="5" onchange="bible_search(true);">
<option value="1">Chapter</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option selected="" value="5">5</option>
</select>
章
1.
2.
3.
4.
5.
我的代码与我给出的示例之间的唯一区别是从数据库中检索章节数。这是我需要把它转换成int的地方,现在可以用了。