搜索栏作为基本模板添加到一个 class 视图 Django
search bar add as a base template into one class views Django
views.py
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
base.html
<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>
在这里,BookList 视图是我的主页,当我从这里搜索时,它运行良好,但当我转到详细信息页面时,它就不起作用了。由于 BookDetailView,我没有在 base.html 模板中发送任何类型的查询。因此,在这种情况下,我如何从 DetailView 发送查询,或者 DetailView 是否有任何内置装饰器,或者我可以只使用一个 Class 来动态搜索所有模板吗?
谢谢
您需要以以下形式指定 BookList
视图的 url:
<form class="example" action="{% url "the name of your route for the BookList" %}" style="margin:auto;max-width:300px">
views.py
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
base.html
<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>
在这里,BookList 视图是我的主页,当我从这里搜索时,它运行良好,但当我转到详细信息页面时,它就不起作用了。由于 BookDetailView,我没有在 base.html 模板中发送任何类型的查询。因此,在这种情况下,我如何从 DetailView 发送查询,或者 DetailView 是否有任何内置装饰器,或者我可以只使用一个 Class 来动态搜索所有模板吗?
谢谢
您需要以以下形式指定 BookList
视图的 url:
<form class="example" action="{% url "the name of your route for the BookList" %}" style="margin:auto;max-width:300px">