I'm trying to create a search error for my ecommerce website. When a user inputs a search that is not in the database, it should return the search error page. Though it seems my else clause isn't working.
I tried putting the else clause in the search.html page, but it keeps giving me errors and it seems when I try to fix the errors, nothing really happens, it stays the same. I expect the search_error.html page to appear when the user inputs a product name that is not in the database. Though I keep getting for example, when I type "hello," the page appears with "Search results for hello." But it should result the search_error.html page. I also tried currently a else clause in my views.py, but it shows the same thing. I think my else clause isn't working and I don't know why.
My views.py:
def search(request):
if 'searched' in request.GET:
searched = request.GET['searched']
products = Product.objects.filter(title__icontains=searched)
return render(request, 'epharmacyweb/search.html', {'searched': searched, 'products': products})
else:
return render(request, 'epharmacyweb/search_error.html')
def search_error(request):
return render(request, 'epharmacyweb/search_error.html')
My urls.py under URLPatterns:
path('search/', views.search, name='search'),
path('search_error/', views.search_error, name='search_error'),
My search.html page:
{% if searched %}
<div >Search Results for {{ searched }}</div>
<div >
{% for product in products %}
<div >
<div >
<img alt="Responsive image" src="{{ product.image.url }}">
<div >
<p >
<a href="{{ product.get_absolute_url }}">{{ product.title }}</a>
</p>
<div >
<small ></small>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
<br></br>
{% else %}
<h1>You haven't searched anything yet...</h1>
{% endif %}
CodePudding user response:
I think you want to check if products = Product.objects.filter(title__icontains=searched)
is returning results instead of checking if "searched" is in the GET
arguments.
To check if the database returned results you can you exists()
https://docs.djangoproject.com/en/4.1/ref/models/querysets/#django.db.models.query.QuerySet.exists
CodePudding user response:
Your if
is returning true because the term 'searched'
is in fact in the request.GET
dictionary. That doesn't mean that there is a product in your database with the value request.GET['searched']
, which might be "hello" when you type in "hello". request.GET
is a dictionary with a key of 'searched'
and a value of "hello". You can also use get()
, which will either get the value of request['searched']
or return None
, so you do not have to check it with an if at all.
Now to check if the database has the value of the search term, you can check the queryset:
def search(request):
# Return the value or None
searched = request.GET.get('searched')
products = Product.objects.filter(title__icontains=searched)
# Check if there are any products with the search term:
if products:
return render(request, 'epharmacyweb/search.html', {'searched': searched, 'products': products})
else:
return render(request, 'epharmacyweb/search_error.html')