分页 Django 3.1 中的空白页
blank page in pagination Django 3.1
我实现了 Django 分页来分割我网站上的搜索结果。
# views.py
def search(request):
# Keywords
if 'keywords' in request.GET:
keywords = request.GET['keywords']
if keywords:
title_list_control_valve = queryset_list.filter(title__icontains=keywords).order_by('title')
description_list_control_valve = queryset_list.filter(description__icontains=keywords).order_by('description').distinct('description')
result_list_control_valves = list(chain(title_list_control_valve,
description_list_control_valve))
result_list_control_valves_2 = list(dict.fromkeys(result_list_control_valves))
paginator = Paginator(result_list_control_valves_2, 1)
page = request.GET.get('page')
paged_queries = paginator.get_page(page)
context = {
'queries': paged_queries,
}
return render(request, 'pages/search.html', context)
else:
return render(request, 'pages/search.html')
else:
return render(request, 'pages/search.html')
显示搜索结果时站点的URL是:
http://localhost:8000/pages/search/?keywords=something
它只在第一页上工作得很好。从第二页开始,它显示一个空白页面,站点的 URL 变为:
http://localhost:8000/pages/search/?page=2
我link到HTML中的其他页面如下:
<div class="col-md-12">
{% if queries.has_other_pages %}
<ul class="pagination">
{% if queries.has_previous %}
<li class="page-item">
<a href="?page={{queries.previous_page_number}} " class="page-link">«</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">«</a>
</li>
{% endif %}
{% for i in queries.paginator.page_range %}
{% if queries.number == i %}
<li class="page-item active">
<a class="page-link">{{i}}</a>
</li>
{% else %}
<li class="page-item">
<a href="?page={{i}} " class="page-link">{{i}}</a>
</li>
{% endif %}
{% endfor %}
{% if queries.has_next %}
<li class="page-item">
<a href="?page={{queries.next_page_number}}" class="page-link">»</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">»</a>
</li>
{% endif %}
</ul>
{% endif %}
</div>
URL 的 ?keywords=something
部分消失了。
有问题吗?
如何让其他页面的 link 也能正常工作?
将 keywords
添加到您的 GET
参数。对于前。 href="?page={{queries.previous_page_number}}&keywords={{request.GET.keywords}}"
我实现了 Django 分页来分割我网站上的搜索结果。
# views.py
def search(request):
# Keywords
if 'keywords' in request.GET:
keywords = request.GET['keywords']
if keywords:
title_list_control_valve = queryset_list.filter(title__icontains=keywords).order_by('title')
description_list_control_valve = queryset_list.filter(description__icontains=keywords).order_by('description').distinct('description')
result_list_control_valves = list(chain(title_list_control_valve,
description_list_control_valve))
result_list_control_valves_2 = list(dict.fromkeys(result_list_control_valves))
paginator = Paginator(result_list_control_valves_2, 1)
page = request.GET.get('page')
paged_queries = paginator.get_page(page)
context = {
'queries': paged_queries,
}
return render(request, 'pages/search.html', context)
else:
return render(request, 'pages/search.html')
else:
return render(request, 'pages/search.html')
显示搜索结果时站点的URL是:
http://localhost:8000/pages/search/?keywords=something
它只在第一页上工作得很好。从第二页开始,它显示一个空白页面,站点的 URL 变为:
http://localhost:8000/pages/search/?page=2
我link到HTML中的其他页面如下:
<div class="col-md-12">
{% if queries.has_other_pages %}
<ul class="pagination">
{% if queries.has_previous %}
<li class="page-item">
<a href="?page={{queries.previous_page_number}} " class="page-link">«</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">«</a>
</li>
{% endif %}
{% for i in queries.paginator.page_range %}
{% if queries.number == i %}
<li class="page-item active">
<a class="page-link">{{i}}</a>
</li>
{% else %}
<li class="page-item">
<a href="?page={{i}} " class="page-link">{{i}}</a>
</li>
{% endif %}
{% endfor %}
{% if queries.has_next %}
<li class="page-item">
<a href="?page={{queries.next_page_number}}" class="page-link">»</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">»</a>
</li>
{% endif %}
</ul>
{% endif %}
</div>
URL 的 ?keywords=something
部分消失了。
有问题吗?
如何让其他页面的 link 也能正常工作?
将 keywords
添加到您的 GET
参数。对于前。 href="?page={{queries.previous_page_number}}&keywords={{request.GET.keywords}}"