Hi I am following the official documentation and setting up pagination for my website, my index template looks like this;
{% for post in list_of_posts %}
<div class="body"><a class="title" href="/post/{{post.id}}"><h2>{{ post.title }}</h2></a>
<P>{{ post.body|truncatewords:50|wordwrap:110 }}</P>
<h3>{{ post.date|date:"jS F Y" }}</h3>
<hr>
</div>
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if post.has_previous %}
<a href="?page={{ post.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ post.number }} of {{ contacts.paginator.num_pages }}.
</span>
{% if post.has_next %}
<a href="?page={{ post.next_page_number }}">next</a>
{% endif %}
</span>
</div>
and my view looks like this;
# Main page
def index(request):
list_of_posts = Post.objects.all().order_by('-date')
list_of_posts = list_of_posts.filter(published=True)
paginator = Paginator(list_of_posts, 10)
page = request.GET.get('page')
try:
post = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
post = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
post = paginator.page(paginator.num_pages)
return render_to_response('index.html', {'list_of_posts': list_of_posts})
I get a stange feeling the TypeError is due to the paginator not outputting any value? My traceback isnt too helpful but here it is
Exception Value:
int() argument must be a string or a number, not 'NoneType'
Exception Location: C:\Python27\lib\site-packages\django\core\paginator.py in validate_number, line 23
TypeError at /
int() argument must be a string or a number, not 'NoneType'
Any guidance on what could be going wrong would be much appreciated.
Add TypeError in your first catch:
except (PageNotAnInteger, TypeError):
# ...
But you also could avoid this error if you will get page number like this:
page = request.GET.get("page", 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With