Skip to content

Instantly share code, notes, and snippets.

@LowerDeez
Created May 28, 2017 21:29
Show Gist options
  • Save LowerDeez/b39f682c786698790e5af81e30263a03 to your computer and use it in GitHub Desktop.
Save LowerDeez/b39f682c786698790e5af81e30263a03 to your computer and use it in GitHub Desktop.
Django Pagination (with search query)
{% for article in articles %}
{% include 'articles/partial/partial_article.html' %}
{% empty %}
<h1>Nothing to show</h1>
{% endfor %}
<!-- Pagination -->
{% if articles.has_other_pages %}
<div class="text-center">
<ul class="pagination pagination-lg text-center">
{% if articles.has_previous %}
<li><a href="?page={{ articles.previous_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">&laquo;</a></li>
{% else %}
<li class="disabled"><span>&laquo;</span></li>
{% endif %}
{% if start_index %}
<li><a href="?page={{ start_index }}">{{ start_index }}</a></li>
<li class="disabled"><span>&mldr;</span></li>
{% endif %}
{% for i in page_range %}
{% if articles.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if articles.has_next %}
{% if end_index %}
<li class="disabled"><span>&mldr;</span></li>
<li><a href="?page={{ end_index }}">{{ end_index }}</a></li>
{% endif %}
<li><a href="?page={{ articles.next_page_number }}
{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">&raquo;</a></li>
{% else %}
<li class="disabled"><span>&raquo;</span></li>
{% endif %}
</ul>
</div>
{% endif %}
<!-- Pagination -->
# pagination looks like:
# 1...3 4 5 6 7 8 9...11, where '6' is active element
def _articles(request, articles):
paginator = Paginator(articles, 4)
try:
page = int(request.GET.get('page', '1'))
except:
page = 1
try:
articles = paginator.page(page)
except PageNotAnInteger:
articles = paginator.page(1)
except EmptyPage:
articles = paginator.page(paginator.num_pages)
# Get the index of the current page
index = articles.number - 1
print(index)
# This value is maximum index of pages, so the last page - 1
max_index = len(paginator.page_range)
print(max_index)
# range of 7, calculate where to slice the list
start_index = index - 3 if index >= 3 else 0
end_index = index + 4 if index <= max_index - 4 else max_index
print(end_index)
# new page range
page_range = paginator.page_range[start_index:end_index]
# showing first and last links in pagination
if index >= 4:
start_index = 1
if end_index-index >= 4 and end_index != max_index:
end_index = max_index
else:
end_index = None
context = {
'articles': articles,
'page_range': page_range,
'start_index': start_index,
'end_index': end_index,
}
if not request.user.is_anonymous:
user_articles = Article.objects.get_published().filter(author=request.user)
if user_articles:
context = {
'articles': articles,
'user_articles': user_articles,
'page_range': page_range,
'start_index': start_index,
'end_index': end_index,
}
return render(request, 'articles/home.html', context)
def home(request):
articles = Article.objects.get_published()
query = request.GET.get('q') # also add {% if request.GET.q %}&q={{ request.GET.q }}{% endif %}
# in template home.html in pagination section
if query is not None and query != '':
articles = articles.filter(
Q(title__icontains=query) |
Q(content__icontains=query) |
Q(short_description__icontains=query) |
Q(author__username__icontains=query)
).distinct()
return _articles(request, articles)
@mat48
Copy link

mat48 commented Jan 22, 2024

Thanks man! Saved me a lot of time! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment