1👍
✅
What you want to do in HTML template is to get current URL. But actually you don’t have to do it – relative path can be used instead. So just remove it:
<li class="paginator-data"><a href="?page={{ page_obj.next_page_number }}">Next</a></li>
The browser knows current URL and will use href
attribute relative to it.
UPD
alternative – to keep other parameters in the URL is to construct "next URL" inside the view and pass it in context as next_url
.
I.e. something like so:
In views:
next_url = request.get_full_path()
next_url.replace(f'page={current}', f'page={page_obj.next_page_number}')
context['next_url'] = next_url
In template:
<li class="paginator-data"><a href="{{ next_url }}">Next</a></li>
Source:stackexchange.com