from django.shortcuts import render, get_object_or_404
from django.utils.decorators import method_decorator
from django.views.decorators.gzip import gzip_page
from django.views.decorators.http import condition
from django.views.generic.detail import SingleObjectMixin
from django.utils import timezone
from django.views.generic import \
ListView, DetailView
from .models import (
Book,
Category,
Author,
Language,
Currency,
Tag,
)
from django.db.models import Q
class BookList(ListView):
model = Book
context_object_name = 'book_list'
template_name = 'books/book_lists.html'
paginate_by = 12
extra_context = {
'category_list': Category.objects.all(),
'author_list': Author.objects.all(),
'language_list': Language.objects.all(),
}
def get_queryset(self):
query = self.request.GET.get('q')
if query:
object_list = self.model.objects.filter(
Q(name_of_the_book__icontains=query) |
Q(author__first_name__icontains=query) |
Q(category__name__icontains=query)
)
else:
object_list = self.model.objects.all()
return object_list
class SingleCategoryView(DetailView):
model = Category
template_name = 'books/single_category.html'
paginate_by = 12
extra_context = {
'category_list': Category.objects.all(),
'author_list': Author.objects.all(),
'language_list': Language.objects.all(),
}
class SingleAuthorView(DetailView):
model = Author
template_name = 'books/single_author.html'
extra_context = {
'category_list': Category.objects.all(),
'author_list': Author.objects.all(),
'language_list': Language.objects.all(),
}
class SingleLanguage(DetailView):
model = Language
template_name = 'books/single_language_list.html'
extra_context = {
'category_list': Category.objects.all(),
'author_list': Author.objects.all(),
'language_list': Language.objects.all(),
}
class BookDetails(DetailView):
model = Book
template_name = 'books/book_details.html'
# extra_context = {
# 'category_list': Category.objects.all(),
# 'author_list': Author.objects.all(),
# 'language_list': Language.objects.all(),
# }
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['now'] = timezone.now()
return context
Here BookList view is my home page. In my base template, I've added some kind of category such as SingleCategoryView, SingleLanguage view etc as dynamic URL dropdown in the navbar.
<!-- nav bar -->
<div class="navbar">
<a href="{% url 'library:book_list' %}">Home</a>
<div class="dropdown">
<button class="dropbtn">Category
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
{% for object in category_list %}
<a href="{% url 'library:single_category_details' object.slug %}">{{ object.name }}</a>
{% endfor %}
</div>
</div>
<div class="dropdown">
<button class="dropbtn">Author
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
{% for object in author_list %}
<a href="{% url 'library:single_author_details' object.slug %}">{{ object.first_name }} {{ object.last_name }}</a>
{% endfor %}
</div>
</div>
<div class="dropdown">
<button class="dropbtn">Language
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
{% for object in language_list %}
<a href="{% url 'library:single_language_list' object.slug %}">{{ object.language }}</a>
{% endfor %}
</div>
</div>
</div>
<!-- nav bar end -->
whilst on the homepage or any other category list page that categories dropdown navbar are showing good but when I'm going to in my BookDetail page it doesn't show. I commented out that codes into BookDetail View.
Please visit this link, you will understand clearly. https://clean-book-library.herokuapp.com/ Keep your cursor author, language or category. You will see the list and then go to the details page of any book and keep your cursor again you won't see that. The main question, why doesn't work BookDetails View code that I've commented out and how to create base search bar into one class views such like as dropdown navbar.
Thanks.
You can pass extra context data in get_context_data
function.
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['now'] = timezone.now()
context.update(self.extra_context)
return context
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.