Reputation:
first of all, thank for reading my question :) I'm currently working on a small Blog platform and i want to implement pagination for all my Post elemets. therefor i modified my view.py and the actual .html template. The problem now is that each time i visit the post_list.html page and i click on next nothing happends.. What the matter here:
post_list.html
{% extends 'quickblog/base.html' %}
{% block content %}
{% for post in posts %}
<br>
<div class="post">
<h1><u><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></u></h1>
<p>{{ post.text|linebreaksbr }}</p>
<div class="date">
<a>published by: {{ post.author }}</a><br>
<a>published at: {{ post.published_date }}</a><br>
<a>tags: {{ post.tag }}</a>
</div>
</div>
{% endfor %}
<div class="pagination">
<span class="step-left">
{% if posts.has_previous %}
<a href="?page=1">« first</a>
<a href="?page={{ posts.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
</span>
{% if posts.has_next %}
<a href="?page={{ posts.next_page_number }}">Next</a>
<a href="?page={{ posts.paginator.num_pages }}">Last »</a>
{% endif %}
</span>
</div>
{% endblock %}
views.py
def post_list(request):
list_posts = Post.objects.get_queryset().order_by('created_date')
paginator = Paginator(list_posts, 15) # Show 15 Posts per page
page = request.GET.get('posts')
posts = paginator.get_page(page)
return render(request, 'quickblog/post_list.html', {'posts': posts})
Upvotes: 1
Views: 64
Reputation: 67
As pagination works on ordered data you need to either append order_by after your query or use default ordering in your models as specified in the below link
Link of another stackowerflow Issue
Hope this helps.
Upvotes: 0
Reputation: 23064
In your template you use page
as query parameter. So you should use that in the view as well.
template:
<a href="?page={{ posts.next_page_number }}">Next</a>
view:
page = request.GET.get('page') # not .get('posts')
Upvotes: 1