I'm developing an app using Django and I made a simple search inside a very big database and a Paginator for the results. The problem is that when I try to go to the second results page, I lose the searched term from my link.
I've tried to rewrite the search word in the input field with JQuery, that didn't work. I tried to re-send the value from the server-side to the input field, didn't work either.
How can I manage to keep it for the second page of results? Any other hints?
This is my code, the relevant part at least:
results.html:
...
<form method="POST" id="searchForm" action="{% url ps.views.search page=1 searchTerm='__search_term__' %}">
{% csrf_token %}
<input type="text" id="billSearched">
<input type="submit" value="{% trans "Look for" %}">
</form>
...
<div class="pagination">
{% if current_page.has_previous %}
<a href="{% url ps.views.search page=current_page.previous_page_number searchTerm='__search_term__' %}">previous</a>
{% endif %}
<span class="current">
Page {{ current_page.number }} of {{ current_page.paginator.num_pages }}
</span>
{% if current_page.has_next %}
<a href="{% url ps.views.search page=current_page.next_page_number searchTerm='__search_term__' %}">next</a>
{% endif %}
</div>
search.py:
def search(request,page,searchTerm):
found_bills = Bill.objects.filter(name__icontains=searchTerm)
searchedWord = str(searchTerm)
paginator = Paginator(found_bills,25)
try:
current_page = paginator.page(page)
except (EmptyPage, InvalidPage):
current_page = paginator.page(paginator.num_pages)
bills_list = list(current_page.object_list)
return render_to_response('results.html',{"bills_list":bills_list,"current_page":current_page,"searchTerm":searchTerm,"searchedWord":searchedWord},context_instance=RequestContext(request))
And also the urls.py althought I'm not sure it's helpful :)
urlpatterns = patterns('',
url(r'^$','ps.views.bills',name="bills"),
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^search/(?P<page>\d+)/(?P<searchTerm>\w*)','ps.views.search',name="search"),)
I should mention that in the address bar the searched term when I go to page 2 is "__search_term__".
i.e. http://127.0.0.1:8000/search/2/__search_term__
Thank you in advance! :)