1

The searchbox gets the data but it doesn't show any results. This is my code so far:

views.py

def search(request):
if 'q' in request.GET and request.GET['q']:
    q = request.GET['q']
    books = Advent.objects.filter(title__icontains=q)
    return render(request, 'search_results.html', {'books': books, 'query': q})
else:
    return HttpResponse('Please submit a search term.')

urls.py

    url(r'^your_url/?$', 'myblog.views.search', name='your_url_name'),

search_results.html

<p>You searched for: <strong>{{ query }}</strong></p>

{% if books %}
<p>Found {{ books|length }} book{{ books|pluralize }}.</p>
<ul>
    {% for book in books %}
    <li>{{ book.title }}</li>
    {% endfor %}
</ul>
{% else %}
<p>No books matched your search criteria.</p>
{% endif %}

index.html

<form type="get" action=".">
<input type="search" id="q" name="q" placeholder="Search..."/>
</form>
5
  • 6
    I don't see anything wrong with the code you posted here. Commented May 2, 2016 at 15:41
  • But it still won't work. Can you suggest something else? Commented May 2, 2016 at 15:45
  • Post the view and the template that the search form comes from; there could be a mistake there...? Commented May 2, 2016 at 15:47
  • Sure, here is the template: <form type="get" action="."> <input type="search" id="q" name="q" placeholder="Search..."/> </form> Commented May 2, 2016 at 15:49
  • Please, edit your question and put that source there. Commented May 2, 2016 at 16:10

2 Answers 2

1

If i correct understand your problem, form send request on incorrect address, change action attribute:

<form type="get" action="{% url 'your_url_name' %}">

and then form will send the request to the correct address and not on the index.html

Sign up to request clarification or add additional context in comments.

Comments

0

I would do something like this:

views.py:

from django.views.decorators.http import require_http_methods

@require_http_methods(['GET'])
def search(request):
    q = request.GET.get('q')
    if q:
        books = Advent.objects.filter(title__icontains=q)
        return render(request, 'search_results.html', {'books': books, 'query': q})
    return HttpResponse('Please submit a search term.')

index.html:

<form type="get" action="{% url 'your_url_name' %}" accept-charset="utf-8">
    <input type="search" id="q" name="q" placeholder="Search..."/>
</form>

The view requires GET methods. You also don't need the else statement, and you can move getting the query outside of the if statment. This will shorten your code a little.

In your HTML, you needed to change your action request to a proper url. If you do not have the write url mapped between your form and your view, it will not know what to do when the form is submitted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.