1

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! :)

Radu Gheorghiu
  • 20,049
  • 16
  • 72
  • 107

1 Answers1

2

Change your form method to GET instead of POST, then build your links with the search term as part of the URL's query-string like this...

<a href="?page={{ contacts.previous_page_number }}">previous</a>

https://docs.djangoproject.com/en/dev/topics/pagination/

  • Can you please give an example on how to build a link for the search term? I'm afraid I don't understand.. – Radu Gheorghiu Apr 27 '12 at 10:18
  • I made my url to look like this, so it passes the searched word and the page, but I can't get the application to print out the results. I guess they don't see the 'searchTerm' and 'page' field in the `GET` data. – Radu Gheorghiu Apr 27 '12 at 10:38
  • Generally you'd use GET when you're getting data from the server and POST when you're sending data to the server. http://stackoverflow.com/questions/46585/when-do-you-use-post-and-when-do-you-use-get I noticed you accepted the answer, did you get this working now? –  May 05 '12 at 20:26
  • Yes, I have. And I understood the difference between the two methods. Thank you again. – Radu Gheorghiu May 05 '12 at 20:40