Answer the question
In order to leave comments, you need to log in
Why is there an error in the search form?
I'm trying to put the search form on crutches, but the template doesn't like the action of the form, TrackBack swears at the context ('products': products) in the product_list function
#views
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request, 'shop/product/list.html',
{'category': category,
'categories': categories,
'products': products})
def search_view(request):
search_quary = request.GET.get('search', '')
if search_quary:
f_products = Product.objects.filter(Q(name__icontains=search_quary) |
Q(descruption__icontains=search_quary))
return render(request, 'shop/product/search.html', {'f_products': f_products})
#urls
path('search/', views.search_view, name='search_view'),
#base.html
<form action="{% url 'search_view' %}" method="get">
<input type="search" name="search" value="">
<button type="submit">Search</button>
</form>
#search.html
{% extends "shop/base.html" %}
{% block content %}
{% for s in f_products %}
{{ s }}
{% endfor %}
{% endblock %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question