简体   繁体   中英

Pagination Django 1.8.1

Im trying to use Paginator in Django to get formatted output. My code is :

def tasks(request):
    rfcs = HandledRFC.objects.all()
    paginator = Paginator(rfcs,2) #2 objects per page
    page = request.GET.get('page')
    try: 
        rfc_per_page = paginator.page(page)
    except PageNotAnInteger:
        rfc_per_page = paginator.page(1)
    except EmptyPage:
        rfc_per_page = paginator.page(paginator.num_pages)
    return render_to_response('tasks.html', { 'rfc_per_page' : rfc_per_page })

And i get EmptyPage exception in browser. Exception value : "Tha page number is less then 1"

Also, in traceback it(Django) links on my "bad" template, so he marked this:

<a href="?page= {{rfc_per_page.previous_page_number }}">Previous</a>

In python shell I tried to emulate this exception generation and it has come when I typed : rfc_per_page = paginator.page(0)

How can I fix this problem?

Thanks for any help!

You should remove the blank space of the href attribute between '=' and '{{':

<a href="?page={{rfc_per_page.previous_page_number }}">Previous</a>

Otherwise, I think it will be replaced by %20 (URL encoding).

You have to put if condition to check whether tha previous page number is available or not?

{% if rfc_per_page.previous_page_number %}
    <a href="?page={{rfc_per_page.previous_page_number }}">Previous</a>
{% endif %}

Let me know whether it is working or not?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM