My site can't run a successful for-loop created in django for a list model that I have sent to a page. I was going based off of Derek's answer in this stackoverflow question and some other things I found. But right now... It seems that my browser (Google Chrome if that helps) can't seem to render the {% for %} loops that I attempt to run in javascript on my html file.
I have two python/django created two list models, booklist_greek and booklist_latin that are sent to my textlist.html file. These lists contain sorted 2-tuples, each tuple having the title of a book as the first element and the type of the book as a second element. Using template-language in html to run a for loop works just fine at two parts in my html file.
There's a point in my code where I would like to filter booklist_greek and booklist_latin based on the booktype of each book, and use that to populate a <select> element. This filtering and populating needs to happen when a user clicks buttons. So I assumed javascript would be necessary... But I also needed template language to somehow filter the lists.
I approached it like so. But this doesn't work.
var latinBooks = $("#latinTextsDropDown");
$("#latinTextsDropDown").children().remove();
{% for each in booklist_latin %};
var bookTitle = {{ each.0 }};
var bookType = {{ each.1 }};
if (bookType == filteringType) {
var opt = document.createElement("option");
opt.value = bookTitle;
opt.name = bookType;
...
$("#latinTextsDropDown").appendChild(opt);
}
{% endfor %};
I even tried reducing the contents of the {% for %} loop to just be the following: console.log({{ each.0 }}) but that failed as well. However if I make the for-loop contain no template-language, so for instance if it only had console.log("hello world!"); then that actually works. Any template language calls to the objects that are being iterated over, and interestingly enough... No java code within my <script> tag runs at all.
So now I need an alternative, or I need to figure out what I'm doing wrong. I understand that what I was doing was pretty odd from the start. But is there another way to iterate over objects within booklist_latin and booklist_greek? And to check what is actually in each tuple with each iteration?