Hopefully this should be a simple one to help me with.
I have a page with a dropdown menu containing three items:
<form method="GET">
<select name="browse">
<option>Cats</option>
<option>Dogs</option>
<option>Worms</option>
</select>
<input type="submit" value="Submit" />
</form>
<!-- Output table -->
<table id="myTable">
<thead>
<tr>
<th>Name</th>
<th>Colour</th>
</tr>
</thead>
<tbody>
{% for object in object_list %}
<tr>
<td>{{ object.name }}</td>
<td>{{ object.colour }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<!-- Pagination controls -->
<div class="pagination">
<span class="page-links">
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span class="page-current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
{% endif %}
</span>
</div>
When the user selects an item and hits submit, they are given the results in a table as generated by the generic ListView:
class Browse(generic.ListView):
template_name = 'app/browse.html'
paginate_by = 25
def get_queryset(self):
queryset = Cats.objects.all()
if self.request.GET.get("browse"):
selection = self.request.GET.get("browse")
if selection == "Cats":
queryset = Cats.objects.all()
elif selection == "Dogs":
queryset = Dogs.objects.all()
elif selection == "Worms":
queryset = Worms.objects.all()
else:
queryset = Cats.objects.all()
return queryset
However, when I attempt to turn a page using the pagination controls, the queryset resets to the first (default) item Cats, because (I think) the form data is reset.
Any idea how to circumvent this problem?
Thanks!
PS: Oh, on that note, is it possible to set the queryset to none to begin with? Much obliged!
UPDATE: When I use pagination on the Cats queryset it works fine so the bug is only displayed on the other two sets.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…