- , URL .
{% if pages.has_previous %}
<li><a href="?page={{ pages.previous_page_number }}">Prev</a></li>
{% endif %}
, .
, django-fitler, django-fitler ( '?') -, ?page=2 .
, , "" "" - django-fitler &page=5 .
@stathoula, , . , -, &page=3.
This seems very simple, but I had to make a small hacker so as not to repeat &page=1again and again inside the query line when the user clicks through the arrows.
In my case, title is used as a filter, so I need to check if it is present there.
Here is a snippet of what I did, works great for my project.
templates / pagination.html
<div class="paginator">
{% with request.get_full_path as querystring %}
<ul class="pagination nav navbar-nav">
{% if pages.has_previous %}
{% if 'title' in querystring %}
{% if 'page' in querystring %}
<li class="paginator {% if pages.number == page %}active{% endif %}">
<a href="{{ querystring|slice:":-7" }}&page={{ pages.previous_page_number }}">Prev</a>
</li>
{% else %}
<li class="paginator {% if pages.number == page %}active{% endif %}">
<a href="{{ querystring }}&page={{ pages.previous_page_number }}">Prev</a>
</li>
{% endif %}
{% else %}
<li class="paginator {% if pages.number == page %}active{% endif %}">
<a href="?page={{ pages.previous_page_number }}">Prev</a>
</li>
{% endif %}
{% endif %}
{% for page in pages.paginator.page_range %}
{% if 'title' in querystring %}
{% if 'page' in querystring %}
<li class="paginator {% if pages.number == page %}active{% endif %}">
<a href="{{ querystring|slice:":-7" }}&page={{ page }}">{{ page }}</a>
</li>
{% else %}
<li class="paginator {% if pages.number == page %}active{% endif %}">
<a href="{{ querystring }}&page={{ page }}">{{ page }}</a>
</li>
{% endif %}
{% else %}
<li class="paginator {% if pages.number == page %}active{% endif %}">
<a href="?page={{ page }}">{{ page }}</a>
</li>
{% endif %}
{% endfor %}
{% if pages.has_next %}
{% if 'title' in querystring %}
{% if 'page' in querystring %}
<li class="paginator {% if pages.number == page %}active{% endif %}">
<a href="{{ querystring|slice:":-7" }}&page={{ pages.next_page_number }}">Next</a>
</li>
{% else %}
<li class="paginator {% if pages.number == page %}active{% endif %}">
<a href="{{ querystring }}&page={{ pages.next_page_number }}">Next</a>
</li>
{% endif %}
{% else %}
<li class="paginator {% if pages.number == page %}active{% endif %}">
<a href="?page={{ pages.next_page_number }}">Next</a>
</li>
{% endif %}
{% endif %}
</ul>
{% endwith %}
</div>
Here is a view, just in case:
App /views.py
def index(request):
condo_list = Condo.objects.all().order_by('-timestamp_created')
condo_filter = CondoFilter(request.GET, queryset=condo_list)
paginator = Paginator(condo_filter.qs, MAX_CONDOS_PER_PAGE)
page = request.GET.get('page')
try:
condos = paginator.page(page)
except PageNotAnInteger:
condos = paginator.page(1)
except EmptyPage:
condos = paginator.page(paginator.num_pages)
return render(request, 'app/index.html', {
'title': 'Home',
'condos': condos,
'page': page,
'condo_filter': condo_filter,
})
Here is a working example:
.