我有一个名为“ Person”的模型(名称列表)。
6个人名为“ Anne”。我的分页是每页2个。
当我在页面上启动“ Anne”的搜索查询时。我得到“ 6个结果” 的响应,它显示前两个结果,并显示“第1页,共3页。下一个。到目前为止,一切都很好。但是当我按“下一步” ,浏览器仅更新为 .... / post_searchtwo /?page = 2 ,但接下来的两个结果将不会显示。
(在PostgreSQL 11中使用Django 2.2.2版)
提前谢谢。
views.py
def post_searchtwo(request):
form = SearchForm()
query = None
results = []
if 'query' in request.GET:
form = SearchForm(request.GET)
if form.is_valid():
query = form.cleaned_data['query']
results = Person.objects.annotate(
search=SearchVector('city', 'last_name', 'first_name')
).filter(search=query)
paginator = Paginator(results, 2) # --- Show 2 items per page
page = request.GET.get('page')
try:
post_list = paginator.page(page)
except PageNotAnInteger:
post_list = paginator.page(1)
except EmptyPage:
post_list = paginator.page(paginator.num_pages)
context = {
'form': form,
'query': query,
'page': page,
'post_list': post_list,
'results': results
}
return render(request,'search/post_searchtwo.html', context)
我的模板/ post_searchtwo.htnl
{% extends 'base.html' %}
{% load static %}
{% block custom_css %}
<link rel="stylesheet" type="text/css" href="{% static 'css/home_styles.css' %}">
{% endblock %}
{% block content %}
<form action="." method="get">
{{ form.as_p }}
<input type="submit" value="Search">
</form>
{% if query %}
<h3>Posts containing "{{ query }}"</h3>
<h3>
{% with results.count as total_results %}
Found {{ total_results }} result{{ total_results|pluralize }}
{% endwith %}
</h3>
{% for post in post_list %}
<h4>
<a href="{{ post.get_absolute_url }}">{{ post.first_name }} {{ post.last_name }}</a>
</h4>
{% empty %}
<p>Theren are no results for your query.</p>
{% endfor %}
<div class="col-6 offset-md-3">
{% include "pagination.html" with page=post_list %}
</div>
{% endif %}
{% endblock %}
... / pagination.htnl
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }}">Previous</a>
{% endif %}
<span class="current">
Page {{ page.number }} of {{ page.paginator.num_pages }}.
</span>
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}">Next</a>
{% endif %}
</span>
</div>
更新:感谢@Daniel Roseman
正如丹尼尔在下面指出的那样(下面)。
... / pagination.htnl
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
<a href="?query={{ query }}&page={{ page.previous_page_number }}">Previous</a>
{% endif %}
<span class="current">
Page {{ page.number }} of {{ page.paginator.num_pages }}.
</span>
{% if page.has_next %}
<a href="?query={{ query }}&page={{ page.next_page_number }}">Next</a>
{% endif %}
</span>
</div>