1
you should have something like the below if you want to stick to CBVs
class BookList(ListView):
model = Book
# the below would show the details of particular chapters for a specific book
class BookDetail(DetailView):
model = Book
#and so on
class chapterList(ListView):
model = Chapter
on your html - link the urls to each book/chapter by using their respective pks
<a href = '{% url "book" pk = obj.pk %}'></a>
1
Ok, let’s say you want to implement your view as a TemplateView
(but return the same result + some additional queries on different models. You could do the following:
# models.py
class Book(models.Model):
...
class Model1(models.Model):
...
class ModelN(models.Model):
...
# views.py
class BookTemplateView(TemplateView):
def get_context_data(self, *args, **kwargs):
ctx = super(BookTemplateView, self).get_context_data(*args, **kwargs)
ctx["books"] = Book.objects.all()
ctx["data_1"] = Model1.objects.all() # you can refine this, of course
...
ctx["data_n"] = ModelN.objects.all() # you can refine this, of course
return ctx
and in Book_list.html
you could write something like (taken from your question):
{% for wb in books %}
<button>{{ wb.book_name }}</button>
</br></br>
{% endfor %}
...
{% for object in data_1 %}
... do something meaningful ...
{% endfor %}
...
{% for object in data_n %}
... do something meaningful ...
{% endfor %}
Notice that this would work exactly the same if BookTemplateView
inherited from ListView
, and then you didn’t need to assign ctx["books"] = Book.objects.all()
(this is the reason why it seems to me a less DRY approach)
Is this close to what you liked to do?
- [Answered ]-Can't send an email in Django 1.9
- [Answered ]-Django is_valid() returning false
- [Answered ]-Django AssertTemplateUsed test fails since Upgrade to Django 1.8
- [Answered ]-Django not displaying correct URL after reverse
- [Answered ]-Adding Additional Hallo.js formatting features to Wagtail CMS RichTextField
Source:stackexchange.com