搜索后,分页的第二页在django中不起作用
搜索结果未出现在第二页中我应该更改什么来解决我的问题?我使用elasticsearch作为搜索引擎 index.html搜索后,分页的第二页在django中不起作用,django,templates,
elasticsearch,view,pagination,Django,Templates,
elasticsearch,View,Pagination,搜索结果未出现在第二页中我应该更改什么来解决我的问题?我使用elasticsearch作为搜索引擎 index.html <ul> {% for i in paginator.page_range %} {% if i <= page_number|add:5 and i >= page_number|add:-5 %}
<ul>
{% for i in paginator.page_range %}
{% if i <= page_number|add:5 and i >= page_number|add:-5 %}
<li class=" {% if i == page_number %} active {% endif %} " >
<a href="?page={{forloop.counter}}">{{forloop.counter}}</a>
</li>
{% endif %}
{% endfor %}
</ul>
如果写入
?page=…
,则?q=…
参数将被“删除”。诀窍是在转到下一个对象时将其添加到查询字符串:
def index(request):
q = request.GET.get('q')
if q:
articles = PostDocument.search().query("match", title=q, )
paginator = Paginator(articles, 5)
page_number = request.GET.get('page', 1)
page_obj = paginator.get_page(page_number)
return render(request, 'index.html', {
'articles': page_obj.object_list,
'paginator': paginator,
'page_number': int(page_number),
'q' : q
})
else:
articles = ''
return render(request, 'index.html', {'articles': articles})
def索引(请求):
q=请求.GET.GET('q')
如果q:
articles=PostDocument.search().query(“匹配”,title=q,)
paginator=paginator(第5条)
页码=request.GET.GET('page',1)
页码=分页器。获取页码(页码)
返回呈现(请求'index.html'{
“文章”:页面对象列表,
“paginator”:paginator,
“页码”:int(页码),
“q”:q
})
其他:
条款=“”
返回呈现(请求'index.html',{'articles':articles})
然后使用以下命令渲染此内容:
<a href="?page={{ forloop.counter }}&q={{ q|urlencode }}">{{forloop.counter}}</a>
对查询进行百分比编码是必要的,例如,如果查询包含问号(?
)、符号(&
)等
<a href="?page={{ forloop.counter }}&q={{ q|urlencode }}">{{forloop.counter}}</a>