Comparing values in django template

Jon

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?

user4025880

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.

edited at
0

Comments

0 comments
Login to comment

Related

Django template comparing string

Django template comparing string

How to obtain dicts key and values in Django template?

Comparing 2 values using template metaprogramming C++

Django template default filter with nullable boolean values

Showing key values Array Items on Django Template

How to get values from the Ajax() into Django template?

Django Template Filters on Session Values?

Assigning Multiple Template Values to a Variable in Django

Template("..").render() in Django 1.8 not allowing numeric values

unique values in django template

Template for extracting POST values in Django

iterate specific dictinary values in django template

dict zip key values django template

Django: can't access values of dictionary in template

Assigning values to Django template variables in javascript

How to enable decimal values in Django template?

Comparing corresponding elements of two lists in Django Template

Django sum values of from a for loop in template

Django - Comparing datetime values in template

Django - Show list datetime values in template

Django display several foreign key values in template

Show only not None values in Django template

Angular - displaying text based on comparing with array values in template

putting dictionary values inside a table in Django template

get checkbox values in django template and pass to view

Finding 2 values if available in a list in a Django Template

Django template comparing strings returning false?

Override the template of a widget and pass values to it with Django 4.0