I have the following array in my 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})
but when I try to compare entries in this list with chapter
it never returns true
template
<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>
I am wondering if I need to convert a string to an int somewhere or something?
Seems like you haven't posted the full code since this works fine for me. Using code you presented, when I navigate to the page rendered from that view, I see have a select
with the value 5
selected. I assume that's what you want? This suggests to me that there's some conflict elsewhere in the view or the template.
While we're here, HTML select
elements do not have a value
attribute so that should be removed. Secondly, you have two option
elements with the same value. Here's the HTML as rendered in the browser.
<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>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments