Thursday, September 29, 2011

Re: Question about QuerySet and Pagination

Ok, so now i have:

template:
{% block coltype %}colMS{% endblock %}

{% block content %}

<h1>Suche</h1>

<form action="" method="GET">
<label for="q">Search: </label>
<input type="text" name="q" value="{{ query|escape }}">
<input type="submit" value="Search">
</form>

{% if query %}
<h2>Resultate für "{{ query|escape }}":</h2>

{% if results %}
<ul>
<table>
<tr>
<th>The Thin Client Name</th>
<th>Thin Client IP</th>
<th>The Thin Client MAC Address</th>
<th>Inventar Nummer</th>
<th>Einstellungen des Thin Clients</th>
</tr>

{% for Thin_Client in thin_client.object_list %}
<tr>
<td>{{ Thin_Client.NAME }}</td>
<td>{{ Thin_Client.IP }}</td>
<td>{{ Thin_Client.MAC }} </td>
<td>{{ Thin_Client.INVENTAR }} </td>
<td><a href=/thin_client/{{ Thin_Client.NAME }}
>Details</a></td>
</tr>
{% endfor %}
</table>
</ul>
{% else %}
<p>No Thin Clients found</p>
{% endif %}

{% endif %}
{% endblock %}
{% block pagination %}

<div class="paginator" style="position: fixed; top:780px; width:
100%;
text-align: center;background-color:
#ADD8E6;
font-size: 14px;">

<span style="font: bold .875em arial">

{% if thin_client.has_previous %}
<a href="?
page={{ thin_client.previous_page_number }}"><img
src="/site_media/images/back_arrow.png" height="14"
width="30" title="Back"></a>
{% endif %}

<span class="page">
Seite {{ thin_client.number }} von
{{ thin_client.paginator.num_pages }}
</span>

{% if thin_client.has_next %}
<a href="?page={{ thin_client.next_page_number }}"><img
src="/site_media/images/arrow.png" height="14" width="30"
title="Forward"></a>
{% endif %}
</span>
</div>
{% endblock %}

view:

def search(request):
query = request.GET.get('q', '')

if query:
qset = (
Q(NAME__icontains=query)
)
results = Thin_Client.objects.filter(qset).distinct()

else:
results = []

paginator = Paginator(results, 25)
try:
page = int(request.POST.get('page', '1'))
except ValueError:
page = 1
try:
thin_client = paginator.page(page)
except (EmptyPage, InvalidPage):
thin_client = paginator.page(paginator.num_pages)

return render_to_response("thin_clients/thin_client_search.html",
{
"results": results,
"query": query,
"thin_client": thin_client
})

urls:
url(r'^$', 'thinco.views.list'),
url(r'^thin_client/$', 'thinco.views.list'),
url(r'^thin_client/page(?P<page>[0-9]+)/$',
'thinco.views.list'),
url(r'^thin_client/(?P<slug>[a-zA-Z0-9_.-]+)/$',
list_detail.object_detail, thin_client_detail),
url(r'^edit/thin_client/(?P<slug>[a-zA-Z0-9_.-]+)/$',
create_update.update_object, thin_client_update),
url(r'^search/$', 'thinco.views.search'),
...

Now if i click next page I get an empty page because:
http://127.0.0.1:8000/search/?page=2,

I probably need a url entry and do i have to give the poaginator the
query or do I have to save the query somewhere?
Also thank you for your help.

On Sep 29, 3:51 pm, Tom Evans <tevans...@googlemail.com> wrote:
> On Thu, Sep 29, 2011 at 8:36 AM, Felix Wagner <hsaldi...@googlemail.com> wrote:
> > Hello,
>
> > I'm currently trying to paginate my results from a search query.
>
> > views.py:
>
> > def search(request):
> >    query = request.GET.get('q', '')
>
> >    if query:
> >        qset = (
> >            Q(NAME__icontains=query)
> >        )
> >        results = Thin_Client.objects.filter(qset).distinct()
>
> >    else:
> >        results = []
>
> >    paginator = Paginator(results, 25)
> >    try:
> >        page = int(request.POST.get('page', '1'))
> >    except ValueError:
> >        page = 1
> >    try:
> >        thin_client = paginator.page(page)
> >    except (EmptyPage, InvalidPage):
> >        thin_client = paginator.page(paginator.num_pages)
>
> >    return render_to_response("thin_clients/thin_client_search.html",
> > {
> >        "results": results,
> >        "query": query,
> >        })
>
> You need to actually pass the paginated page ('thin_client') to your
> template and iterate over that. You are still iterating over the
> unpaginated 'results', so unsurprisingly you don't see the pagination.
> See the view example in the docs:
>
> https://docs.djangoproject.com/en/1.3/topics/pagination/#using-pagina...
>
> Cheers
>
> Tom

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment