I'm trying to paginate results from a search with multiple options.
The only problem is, once I hit the next button it clears the query
and I'm left paginating all the objects in the DB.
Here is my view:
def filter_search(request):
if request.POST:
reps = Rep.objects.all()
else:
reps = []
query = request.POST
if 'q' in request.POST:
q = request.POST.get('q', '')
qset = (
Q(Last_Name__icontains=q) | Q(First_Name__icontains=1)
)
reps = reps.filter(qset)
else:
q = []
if len(q):
qlist = q
else:
qlist = []
if 'party' in request.POST:
party = request.POST.getlist('party')
reps = reps.filter(Party__in=party)
else:
party = []
if len(party):
plist = party
else:
plist = []
paginator = Paginator(reps, 15)
results = paginator.count
try:
page = int(request.POST.get('page', '1'))
except:
page = 1
try:
repList = paginator.page(page)
except (EmptyPage, InvalidPage):
repList = paginator.page(paginator.num_pages)
finalq = request.POST
return render_to_response("Government/search.html", {
'reps': repList,
'query': query,
'plist': plist,
'qlist': qlist,
'results': results,
'finalq': finalq
})
Here is my tpl:
<form action="" method="POST"><br />
<input type="text" name="q"><br />
Democrat <input type="checkbox" name="party" value="D">
Republican <input type="checkbox" name="party" value="R">
<input type="submit" value="Search">
</form>
{% if query %}
<h3>Your search for {% if qlist %} <span>{{ qlist}}</span> {% endif %}
{% if plist %} {% for p in plist %} {% if forloop.first %} <span>Party
({{ p }})</span> {% else %} & <span> Party ({{p}})</span> {% endif %}
{% endfor %}{% endif %} returned <span>{{ reps.paginator.count }}</
span> result(s)</h3>
<ul>
{% for object in reps.object_list %}
<li>{{ object.Position }} {{ object.First_Name }}
{{ object.Last_Name }} <a href="http://django.newsok.com/government/
reps/{{ object.Last_Name }}_{{ object.First_Name}}">view more</a></li>
{% endfor %}
</ul>
<div id="pagination">
{% if reps.has_previous %}
<a class="activePrev"
href="&page={{ reps.previous_page_number }}">Previous</a>
{% else %}
<a class="nullPrev">Previous</a>
{% endif %}
{% if reps.has_next %}
<a class="activeNext" href="&page={{ reps.next_page_number }}">Next</
a>
{% else %}
<a class="nullNext" href="">Next</a>
{% endif %}
</div>
That href is the cause of the problem, how do I pass a page to the url
without resetting the query?
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.