我在模板中有一个类别下拉列表。我不想对其进行硬编码并为每个类别编写单独的列表视图。那么有没有办法将值传递<a href="">给视图?我想self.request.GET.get('category search')永远不会奏效,因为没有<form method="get">标签。因此它总是None由于return queryset.none()声明而返回。
主页.html:
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
{% if all_categories %}
{% for category in all_categories %}
<a name="category search" href="{% url 'book:category_search' category.id %}"> {{ category }} </a>
{% endfor %}
{% endif %}
</div>
book.urls.py:
urlpatterns = [
.........
path('categories/<int:pk>', views.CategorySearchView.as_view(), name='category_search'),
]
book.views.py:
class CategorySearchView(generic.ListView):
template_name = 'book/search.html'
model = Book
context_object_name = 'book_list'
paginate_by = 100
def get_queryset(self):
queryset = super().get_queryset()
search = self.request.GET.get('category search')
if search:
queryset = Book.objects.filter(categories__id=search)
return queryset
else:
return queryset.none()
相关分类