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>
<form type="get" action="."> <input type="search" id="q" name="q" placeholder="Search..."/> </form>