Вам необходимо указать категорию в контексте шаблона или использовать другой шаблон. Учитывая, что вы возвращаете список, я не совсем уверен, как вы собираетесь вытащить из него категорию.
Если вы предполагали, что это будет прямая ссылка на каждый из элементов, я рекомендуем не использовать title
в качестве поискового объекта. Вместо этого добавьте SlugField в вашу модель и отметьте его как уникальный. Затем используйте это в своем URL:
path('search/<slug:slug>/', item_view, name='item_view'),
Тогда ваш вид будет:
def item_view(request, slug):
item = Item.objects.select_related('category').get(slug__iexact=slug)
return render(request, "search.html", {
'item_list': [item],
'category': item.category
})
Редактировать:
def SearchView(request, category_id=None):
if category_id:
category = Category.objects.get(pk=category_id)
item_list = Item.objects.filter(category__id=category_id)
else:
item_list = Item.objects.all()
title = request.GET.get('title')
if title:
item_list = item_list.filter(title__iexact=title)
else:
query = request.GET.get('q')
if query:
item_list = item_list.filter(title__icontains=query)
price_from = request.GET.get('price_from')
price_to = request.GET.get('price_to')
item_list = item_list.annotate(
current_price=Coalesce('discount_price', 'price'))
if price_from:
item_list = item_list.filter(current_price__gte=price_from)
if price_to:
item_list = item_list.filter(current_price__lte=price_to)
context = {
'item_list': item_list,
'category': category,
}
return render(request, "search.html", context)
URL:
path('search/', SearchView, name='search'),
path('search/<int:category_id>', SearchView, name='category_search'),
path('bike-category/<int:category_id>', category_view, name='category'),
Шаблон:
<div class="offset-md-1 col-md-2">
<h2>Content Filter</h2> <br>
<form method="GET" action="{% if category %}{% url 'core:category_search' category.id %}{% else %}{% url 'core:search' %}{% endif %}">
<h5>Search</h5>
<div class="form-row">
<div class="form-group col-8">
<div class="input-group">
<input class="form-control py-2 border-right-0 border" type="search" name="q"
placeholder="Brand..">
<span class="input-group-append">
<div class="input-group-text bg-transparent">
<i class="fa fa-search"></i>
</div>
</span>
</div>
</div>
</div>
<h5>Price from</h5>
<div class="form-row">
<div class="form-group col-5">
<div class="input-group">
<input class="form-control py-2 border-right-0 border" type="search" name="price_from"
placeholder="Price from" value="{{request.GET.price_from}}">
<span class="input-group-append">
<div class="input-group-text bg-transparent">
<i class="fa fa-search"></i>
</div>
</span>
</div>
</div>
</div>
<h5>Price to</h5>
<div class="form-row">
<div class="form-group col-5">
<div class="input-group">
<input class="form-control py-2 border-right-0 border" type="search" name="price_to"
placeholder="Price to" value="{{request.GET.price_to}}">
<span class="input-group-append">
<div class="input-group-text bg-transparent">
<i class="fa fa-search"></i>
</div>
</span>
</div>
</div>
</div>
<div class="form-row">
<button type="submit" class="btn btn-outline-primary btn-md">Search</button>
</div>
</form>
</div>
<ul>
{% for item in item_list %}
<li>
<a href="{% if category %}{% url 'core:category_search' category.id %}{% else %}{% url 'core:search' %}{% endif %}?title={{item.title}}">
{{ item.title }}
</a>
</li>
{% endfor %}
</ul>
Мне кажется, я правильно обработал атрибут действия формы и атрибуты ссылки href. Возможно, вам придется очистить их для вашей ситуации.