I have many books in a database. Searching the database returns nothing(empty queryset).
In the menu:
<form name="myform" method="POST" action="{% url 'search' %}">
{% csrf_token %}
<input type="text" name="search" placeholder="Search" />
</form>
views.py,
def search(request):
if request.method == 'POST':
book_name = request.POST.getlist('search')
try:
status = Add_prod.objects.filter(bookname__icontains=book_name)
#Add_prod class contains a column called 'bookname'
except Add_prod.DoesNotExist:
status = None
return render(request,"search.html",{"books":status})
else:
return render(request,"search.html",{})
My template file,
{% if books %}
<a href="{% url 'buy_book' pk=books.pk %}">
<!--Upon successful search book image with hyperlink appears -->
<img src="{{books.image.url}}" alt="No Image"></a>
<p>{{books.bookname}}</p>
<p>Rs.{{books.price}}</p>
{% endif %}
There are couple of things I would like to add here..
1) I think search result should be of method GET not POST. You might refer this link to see when to use POST and when to use GET
<form name="myform" method="GET" action="{% url 'search' %}">
// see if you need csrf_token here when this is GET request. Your homework
2) views.py
def search(request):
if request.method == 'GET': # this will be GET now
book_name = request.GET.get('search') # do some research what it does
try:
status = Add_prod.objects.filter(bookname__icontains=book_name) # filter returns a list so you might consider skip except part
return render(request,"search.html",{"books":status})
else:
return render(request,"search.html",{})
3) django template should be now
{% if books %}
{% for each_book in books %} // books is list here so pick each element using for loop
<a href="{% url 'buy_book' pk=each_book.pk %}">
<!--Upon successful search book image with hyperlink appears -->
<img src="{{each_book.image.url}}" alt="No Image"></a>
<p>{{each_book.bookname}}</p>
<p>Rs.{{each_book.price}}</p>
{% endfor %}
{% endif %}
This should be working fine.
Also make you sure you have some data in db to fetch you some result on search!
Your form in the main HTML from where you will use the search option should something like this:-
<form name="myform" method="GET" action="{% url 'search' %}">
<input type="text" name="search" placeholder="Search" /></form>
You need to edit your views.py as
def search(request):
if request.GET.get('myform'): # write your form name here
book_name = request.GET.get('myform')
try:
status = Add_prod.objects.filter(bookname__icontains=book_name)
return render(request,"search.html",{"books":status})
except:
return render(request,"search.html",{'books':status})
else:
return render(request, 'search.html', {'books':status})
And your Django template should look like this:-
{% if books %}
{% for each_book in books %}
<a href="{% url 'buy_book' pk=each_book.pk %}">
<img src="{{each_book.image.url}}" alt="No Image"></a>
<p>{{each_book.bookname}}</p>
<p>Rs.{{each_book.price}}</p>
{% endfor %}
{% endif %}
By doing this your code work perfectly fine by then...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With