I've puzzled my way into a corner. Figuring this will be a
palm-to-forehead moment.

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
}}">&lt;&lt;</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 }}">&gt;&gt;</a>
        {% endif %}
    </span>
</div>

Thx,

-- 
Darren Spruell
phatbuck...@gmail.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to