У меня есть несколько фотографий с тегами на моей странице, и я хотел бы, чтобы они использовались для фильтрации по категории. Таким образом, после нажатия на одно из изображений будет отфильтрован результат по категории.
Так что это будет фильтр, который я хотел бы подключить к одному из изображений:
item_list = item_list.filter(category__pk=1)
html
<div class="col-md-4 overlay zoom">
<a href="/">
<div style="position:relative;">
<img src="{% static '/img/category_choice/bike1.png' %}" class="img-fluid">
<div class="card-img-overlay">
<h2 class="card-title"
style="text-align: center; color: aliceblue; position: absolute; bottom:5px;">
Gravel Bikes
</h2>
</div>
</div>
</a>
</div>
плюс список в конце html файл
<ul>
{% for item in items %}
<li>
{{ item.title }}
</li>
{% endfor %}
</ul>
views.py
def BikeView(request):
query = request.GET.get('q')
if query:
item_list = Item.objects.all()
item_list = Item.objects.filter(title__icontains=query)
context = {
'items': item_list
}
return render(request, "bikes.html", context)
return render(request, "bikes.html", {})
url
urlpatterns = [
path('checkout/', CheckoutView.as_view(), name='checkout'),
path('order-summary/', OrderSummaryView.as_view(), name='order-summary'),
path('product/<slug>/', ItemDetailView.as_view(), name='product'),
path('add-to-cart/<slug>/', add_to_cart, name='add-to-cart'),
path('add-coupon/', AddCouponView.as_view(), name='add-coupon'),
path('remove-from-cart/<slug>/', remove_from_cart, name='remove-from-cart'),
path('remove-item-from-cart/<slug>/', remove_single_item_from_cart,
name='remove-single-item-from-cart'),
path('payment/<payment_option>/', PaymentView.as_view(), name='payment'),
path('request-refund/', RequestRefundView.as_view(), name='request-refund'),
path('bikes/', BikeView, name='bikes'),
path('', HomeView, name='home'),
path('search/', SearchView, name='search'),
path('bike-category/<category_id>', category_view, name='category')
]
просмотров
def category_view(request, category_id):
item_list = Item.objects.filter(category__pk=category_id)
return render(request, "bikes.html", {'item_list': item_list})