Reputation: 241
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
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['now'] = timezone.now()
context.update(self.extra_context)
return context
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()
context.update(self.extra_context)
print(context)
return context
<form class="example" action="" style="margin:auto;max-width:300px">
<input type="text" name='q' placeholder="Search...">
<button type="submit" value='{{ request.GET.q }}'><i class="fa fa-search"></i></button>
</form><br>
Here, BookList view is my homepage and when I'm searching from here then it's working good but when I'm going to Detail page then it doesn't work. Cause from BookDetailView I didn't send any kinds of a query in the base.html template. So, in this case, How can I send a query from DetailView or is there any built-in decorator for DetailView or can i use just one Class that will work for searching dynamically all template?
Thanks
Upvotes: 0
Views: 531
Reputation: 50786
You need to specify the url of your BookList
view in the form:
<form class="example" action="{% url "the name of your route for the BookList" %}" style="margin:auto;max-width:300px">
Upvotes: 1