0

I have this view function for my blog posts.

def home(request):
    posts = Post.objects.all().order_by("-pub_date")
    paginator = Paginator(posts, 5)

    try: page = int(request.GET.get("page", 1))
    except ValueError: page = 1

    try: posts = paginator.page(page)
    except (InvalidPage, EmptyPage):
        posts = paginator.page(paginator.num_page)

    return render_to_response("home.html",
                            dict(posts=posts, user=request.user))

In my main urls.py file, I have the following:

urlpatterns = patterns('',
    url(r'^', 'blog.views.home', name='home'),
    url(r'^admin/', include(admin.site.urls)),
)

Also, I didn't create a urls.py file under my app folder.

And I have this in my home.html file.

{% block footer %}

<ul class="pager">
{% if posts.object_list and posts.paginator.num_pages > 1 %}
  {% if posts.has_previous %}
  <li><a href="{{ posts.previous_page_number }}">Previous</a></li>
  {% endif %}
  {% if posts.has_next %}
  <li><a href="{{ posts.next_page_number }}">Next</a></li>
  {% endif %}
</ul>
{% endif %}

</div><!-- /.blog-main -->
{% endblock %}

Up to this point, the homepage (home.html) renders properly on my local server. However, when I click on next page link, it sends me to

127.0.0.1:8000/2

and shows the same page. Also, the previous button is not showing.

Question: Have I done anything wrongly? And how do I direct my views to page 2 properly in the url dispatcher?

Help appreciated. Thank you.

codemax
  • 1,322
  • 10
  • 19

1 Answers1

0

you can use the documentation's version of pagination view. https://docs.djangoproject.com/en/dev/topics/pagination/

malisit
  • 1,253
  • 2
  • 17
  • 36