Thursday, September 29, 2011

Question about QuerySet and Pagination

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,
})

thin_client_search.html:


{% block coltype %}colMS{% endblock %}

{% block content %}
<h1>Search</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>Results for "{{ 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 results %}
<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:750px; width:
100%;
text-align: center;background-color:
#ADD8E6;
font-size: 14px;">

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

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

<span class="page">
Page {{ results.number }} of
{{ results.paginator.num_pages }}
</span>

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

urls.py:
...
url(r'^search/$', 'thinco.views.search'),
...


Now It just won't work and I have to admit I don't quite understand
why. Any help is appreciated.

--
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