I've created this function at my views to iterate through my pages.
for chapter in chapters:
context["chapter_page"] = math.ceil((chapters.index(chapter) 1) / 2)
context["chapter"] = chapters
return context
I am still making a for a loop in my template, so I cannot remove him. I added this context, but the only returned page is the last page, which means, that my context["chapter_page"] is not iterating.
{% for chapter in chapters %}
<li>
<a
href="?page={{ chapter_page }}&#{{ chapter.number }}">
{{ chapter.number }}
</a>
</li>
{% endfor %}
Of course, I could not add this logic direct to my template, it is not accepted by Django.
{% for chapter in chapters %}
<li>
<a
href="?page={{ math.ceil((chapters.index(chapter) 1) / 2) }}&#{{ chapter.number }}">
{{ chapter.number }}
</a>
</li>
{% endfor %}
I am expecting to do a loop and return each iterated number at my href=page
CodePudding user response:
I'm guessing here, because you have omitted a bunch of details, but it looks like you are expecting one chapter_page
value per chapter. That's not what you have implemented. You have exactly one global chapter_page
value.
If you want one per chapter, then that number needs to be part of the chapter
object, not global:
for idx,chapter in enumerate(chapters):
chapter["chapter_page"] = math.ceil((idx 1) / 2)
context["chapter"] = chapters
return context
and
{% for chapter in chapters %}
<li>
<a
href="?page={{ chapter.chapter_page }}&#{{ chapter.number }}">
{{ chapter.number }}
</a>
</li>
{% endfor %}