Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 比较django模板中的值_Python_Django_Django Templates - Fatal编程技术网

Python 比较django模板中的值

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

我的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_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
。我想这就是你想要的?这对我来说意味着视图或模板中的其他地方存在一些冲突

在这里,HTML
select
元素没有
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的地方,现在可以用了。