How can I create a shortcut to go to a section on another page?
I have a book as an object and I am paginating its chapters, but, its chapters are OBJECTS, with chapter.number and chapter.story. I have a book summary with all the chapters’ numbers, but, thus summary should be a shortcut to the page, for example, book/chapter?page=5&#chapter_five.
class ChapterView(DetailView):
template_name = "tailwind/book_detail.html"
model = Books
slug_url_kwarg = "book_name"
slug_field = "name"
def get_object(self, queryset=None):
try:
return Books.objects.get(name=self.kwargs["book_name"])
except Books.DoesNotExist as e:
raise Http404
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
tuple_book = self.get_object().ordered_chapter().items()
book = [{"chapter_number": chapter_number, "chapter_resume": chapter_resume} for chapter_number, chapter_resume in tuple_book]
page = self.request.GET.get("page", 1)
paginator = Paginator(book, 2)
try:
paginated_book = paginator.page(page)
except PageNotAnInteger:
paginated_book = paginator.page(1)
except EmptyPage:
paginated_book = paginator.page(paginator.num_pages)
# just get the current page
# the book is not paginated because I need to return all indexes in the template
for chapter in book.object_list:
if self.request.GET.get("chapter_number", None) == chapter.chapter_number:
context["chapter_page"] = chapter["chapter_number"].get_page_number()
context["book"] = book
context["paginated_book"] = paginated_book
return context
Now, this is my template, this is the part where I am returning the paginated chapters.
<div>
<div>
{% for chapter in paginated_changelogs %}
<div>
<div>
<h2>{{ chapter.chapter_number }} </h2>
<div>
{{ chapter.chapter_resume }}
</div>
</div>
</div>
{% endfor %}
</div>
And this is my book index, where I should go to a specific section, even if it is on another page:
<ul>
{% for chapter_number in book %}
<li>
<a
href="?page={{ chapter_page }}&#{{ chapter_number }}">
{{ chapter_number }}
</a>
</li>
{% endfor %}
</ul>
However, this is not working because the function does not return a page. It returns nothing, I get book/chapter?page=&#chapter_five.
I tried to implement this answer How can I create a book index with Python?, but something works different.