Reputation: 43
Searching result is not appearing in second page what should I change to solve my problem ? and I'm using elasticsearch as search engine index.html
<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>
and this is in my views.py
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),
})
else:
articles = ''
return render(request, 'index.html', {'articles': articles})
Upvotes: 1
Views: 432
Reputation: 477666
If you write ?page=…
, then the ?q=…
parameter is "dropped". The trick is to add it to the querystring when you go to the next object:
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})
and then render this with:
<a href="?page={{ forloop.counter }}&q={{ q|urlencode }}">{{forloop.counter}}</a>
The |urlencode
template filter [Django-doc] is necessary to percentage encode the query, for example if it contains a question mark (?
), ampersand (&
), etc.
Upvotes: 1