Для vvxhid, который мне очень помог.
Я добавляю главу, чтобы легче было найти страницу
Как сделать так, чтобы при выборе главы на сайте динамически появлялись связанные страницы с этой главой?
models.py
class Page(models.Model):
title_title = models.ForeignKey('Book', on_delete=models.PROTECT)
chapter_chapter = models.ForeignKey('Chapter', on_delete=models.PROTECT)
number_page = models.CharField(max_length=(500))
def __str__(self):
return self.number_page
class Chapter(models.Model):
book_name = models.ForeignKey(
'Book', on_delete=models.CASCADE, null=True)
chapter_name = models.CharField(max_length=(500), db_index=True)
def __str__(self):
return self.chapter_name
views.py
def Second_page(request, book_slug):
page = Page.objects.all()
chapter = Chapter.objects.all()
page = Page.objects.filter(title_title__slug=book_slug)
chapter = Chapter.objects.filter(book_name__slug=book_slug)
get_object_or_404(Book, slug=book_slug),
return TemplateResponse(request, "second.html", {'page': page, 'chapter': chapter})
second.html
{% extends 'index.html' %}
{% block content %}
<div class="chapter">
<select>
{% for c in chapter %}
<option value="">{{c.chapter_name}}</option>
{% endfor %}
</select>
</div>
{% for p in page %}
<ul>
<li><p>{{p.number_page}}</p></li>
</ul>
{% endfor %}
{% endblock %}