I have a page on my site which is Paginator-ed for the object list.
I'm sticking to the vanilla setup from the docs so when it is in
effect I've got a URI query string of '?page=%d' processed via GET.
I've added a search form to the page and am passing the form data via
GET as well so when a search is in effect I have '?search=%s' on the
URI query. I'm incredibly creative (not) so most of this is done very
closely modeling the approach at
http://www.djangobook.com/en/1.0/chapter07/. I can do either of them
individually just fine, but I can't figure a clean way to have both
pagination and search parameters operate properly across requests as
the user selects next/previous, etc. in the page navigation. It works
if I manually append the missing query parameter to the URI.
How is this typically best handled in Django? I thought about trying
to determine uri query in template and dynamically build the nav links
to include parameters for search and pagination but I didn't figure a
way that wasn't a complete mess.
## view
def list_submissions(request):
"""
Present list of submission logs, paginated.
"""
search_form = SubmissionLogSearchForm()
query = request.GET.get('search', '')
if query:
qset = (
Q(file_name__icontains=query) |
Q(file_md5=query) |
Q(file_sha1=query) |
Q(submitter__username=query)
)
submissionlog_list = SubmissionLog.objects.filter(qset)
else:
submissionlog_list = SubmissionLog.objects.all()
paginator = Paginator(submissionlog_list, 15) # show N logs per page
# Make sure page request is an int. If not, deliver first page.
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
# If page request (9999) is out of range, deliver last page of results.
try:
submissionlogs = paginator.page(page)
except (EmptyPage, InvalidPage):
submissionlogs = paginator.page(paginator.num_pages)
return render_to_response('avsubmit/submissionlog_pages.html', {
'submissionlogs': submissionlogs,
'search_form': search_form,
}, context_instance=RequestContext(request))
## template
<h4>Showing {{ submissionlogs.object_list.count }} submission{{
submissionlogs.object_list.count|pluralize }} of {{
submissionlogs.paginator.count }} total</h4>
{% if submissionlogs %}
<form action="{% url submissionlog_list %}"
method="get">
{{ search_form.as_p }}
<input type="submit" value="Search" />
</form>
<table>
<tr class="tableheader">
<th>File MD5</th><th>File
Name</th><th>Submitter</th><th>Submission Date</th>
</tr>
{% for log in submissionlogs.object_list %}
{% cycle 'row1' 'row2' as rowcolors silent %}
<tr class="{{ rowcolors }}">
<td style="font-family: monospace; font-weight: bold; font-size:
14px;"><a href="{% url submissionlog_detail log.id %}" t
itle="{{ log.file_name }} - {{ log.file_magic }}">{{ log.file_md5 }}</a></td>
<td>{{ log.file_name }}</td>
<td>{{ log.submitter }}</td>
<td>{{ log.date_submitted|date:"m/d/Y h:i A" }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
<div class="pagination">
<span class="step-links">
{% if submissionlogs.has_previous %}
<a href="?page={{ submissionlogs.previous_page_number
}}"><<</a>
{% endif %}
<span class="current">
Page {{ submissionlogs.number }} of {{
submissionlogs.paginator.num_pages }}
</span>
{% if submissionlogs.has_next %}
<a href="?page={{ submissionlogs.next_page_number }}">>></a>
{% endif %}
</span>
</div>
Thx,
--
Darren Spruell
phatb...@gmail.com
Is the request.GET.get() method working properly? Does the test on
query work? I suspect one of these places doesn't work as expected.
I'd do similar to the following in my code. (It works on Django 1.3.1
for request.POST, anyway.)
if 'page' in request.GET:
# do pagination
elif 'search in request.GET:
# do search
else:
# do something else
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQIcBAEBAgAGBQJPI8RSAAoJEH+72ZVdir1GW9AP/36bfx6HMDWdsB+AQevDW/ao
ccHy2FzmqI+yEEREoIsuV2fRiVtVwdVoUAI3pkEz4zpspVmDj+f4MhOnPZc+t88l
EBXtCuIpjlHlLK0irRq8AZ3Yw5FnWg8Pm3D7gDVgSSWvgmXem5wXwf8r3s3JjFAU
ovRVWESamRR9sy8TjC/aGJ/5yT2Z5DcDRoTGn4/kCklKFCNvNxkrtO9tw10fjLCb
IeMPGtEP8NkTjXM87//fCOsgJOuet2SwoUYPB9PvpVLQFa8b4yS+vBpCcfWNIwEv
z7pIgksr0ka+xKV4wj1CAieAd7m7pNtWXLzfzWtHTEtQn+0V67Fv1TynE4cQdMM7
2BwZCw3nMMBfcH1XoHte/bqrYzjzqFqRp34H3mTRotrnP8OWc8T1DSeJnZ6CBTT5
zL7H2NSwyBl3JcMOWgyJ6MoHuy6Vv2uWr6OPQcp+P53a19ok5MKeRhnxem26uTV3
truAUUve4gb2MzyP77JGhmzjHzzKM/HD8Z362NVhuk4Bmp8FEyHHyAzSIuTCPfD2
9kb4X+ISxhxwXQLXQRmBTe4gLE8YlsXxiP3vAZnJRoYnJWRl5RrVezrofZLI+Z8o
xtIYugGa+yMp3gBRVCTOxf6TLc9XhHGcLiqoSljYpSfGHEoGzVPJq7Bn8Jho8ite
tbSmLYW73NaMLcBMFkzN
=3Bbe
-----END PGP SIGNATURE-----