I'm trying to make a simple search and return results in a paginated form. Whenever I try to go to the second page, my search term is lost and thus my second page has no results.
I've found and followed the Pagination example in the Djangoproject tutorial, but I haven't found an example on how to write my URL for the search view.
I've used POST method in my form previously, for when I had little data to display (although now, after a bit of research, I know the usage difference between GET and POST). When I gained lots more data, I was constrained to use Pagination. Thus, I've changed my form method to GET but here is my problem, I don't know how to describe my URL so the data is sent to the right view.
I've tried to make it work with POST but with no success. Finally I decided to stick to GET example but stumbled on this URL thing that's keeping me back.
Here is the code in the template and the URLs file:
search.py:
<form method="GET" id="searchForm" action="/search/?page=1">
{% csrf_token %}
<input type="text" id="billSearched" name="q_word">
<input type="submit" value="{% trans "Look for" %}">
</form>
urls.py:
urlpatterns = patterns('',
url(r'^$','ps.views.bills',name="bills"),
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^search/$','ps.views.search',name="search"),)
I've tried many forms for the URL but none have succeeded.
i.e.:
url(r'^search/(?P<page>\d+)/$','ps.views.search',name="search")
url(r'^search/','ps.views.search',name="search")
url(r'^search/(?P<page>\d+)/(?P<searchTerm>\w*)','ps.views.search',name="search")
Any explanation / solution would be really appreciated. Thank you in advance!
UPDATE:
def search(request):
searchTerm = ""
page = 1
import pdb
pdb.set_trace()
if 'q_word' in request:
searchTerm = request.GET['q_word']
if 'page' in request:
page = request.GET['page']
found_bills = Bill.objects.filter(name__icontains = searchTerm)
paginator = Paginator(found_bills,25)
try:
current_page = paginator.page(page)
except PageNotAnInteger:
current_page = paginator.page(1)
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,},context_instance=RequestContext(request))
UPDATE #2:
If I use pdb I can see that there is no data being passed from the client to the server. Got to work on that, but still, any information and/or hints would be really appreciated as they can shorten my search time :)
(Pdb) request.GET
<QueryDict: {u'page': [u'1']}>