I have a search view that allows the user to search for their notecards, and then displays the results paginated 6 pages at a time.
I use pagination in all my other views and it works correctly. However, when using it with a query, it displays the first 6 results, displays "Page 1 of 2" and the next button, but when you click "Next", the results are not obtained.
I set the query "*" as the search object, and when I search for this query, the first call request:
http://127.0.0.1:8000/search/?q=*
When I click next, the call:
http://127.0.0.1:8000/search/?page=2
Search URL:
url(r'^search/', 'notecard.search.views.search', name="search"),
Search Type:
@login_required(login_url='/auth/login/')
def search(request):
query = request.GET.get('q', '')
results = []
notecard_list = []
if query:
if query == "*":
results = Notecard.objects.filter(section__semester__user=request.user)
else:
results = Notecard.objects.filter(Q(section__semester__user=request.user), Q(notecard_body__icontains=query)|Q(notecard_name__icontains=query))
paginator = Paginator(results, 6)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
notecard_list = paginator.page(page)
except (EmptyPage, InvalidPage):
notecard_list = paginator.page(paginator.num_pages)
return list_detail.object_list(
request,
queryset = Notecard.objects.filter(section__semester__user=request.user),
template_name = "notecards/search.html",
template_object_name = "results",
extra_context = {"results": results, "notecard_list": notecard_list,},
)
and search pattern:
{% extends "base.html" %}
{% block title %} Notecard List | {% endblock %}
{% block content %}
<div id='main'>
<table id='notecards'>
<tr>
<th class="name">Search Results</th>
</tr>
</table>
<table id='known_split'>
<p>Search results:<a class="edit" href="{% url semester_list %}">Back to Semesters</a></p>
{% for result in notecard_list.object_list %}
<tr>
<td class="image">{% if result.known %}<img src="{{ STATIC_URL }}/images/known.png" width="41" height="40" />{% else %}<img src="{{ STATIC_URL }}/images/unknown.png" width="41" height="40" />{% endif %}</td>
<td class="text"><a href="{% url notecards.views.notecard_detail result.id %}">{{ result.notecard_name|truncatewords:9 }}</a></td>
</tr>
{% endfor %}
</table>
<div class="pagination">
<span class="step-links">
{% if notecard_list.has_previous %}
<a class="navlink" href="?page={{ notecard_list.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ notecard_list.number }} of {{ notecard_list.paginator.num_pages }}
</span>
{% if notecard_list.has_next %}
<a class="navlink" href="?page={{ notecard_list.next_page_number }}">next</a>
{% endif %}
</span>
</div>
{% endblock %}
, , , - .