django pagination based on view class

django pagination based on view class

Welcome to my blog. Help yourself.
----- perrin

2018-10-09 22:50


Simple is better. Just set the class field 'paginate_by' and make the job done.

For some complicated version, we need to deal with get/post request and override the get/post method. It is for some more complicated situation, such as that the object lists consist of records from serveral tables. As for it ,we need to initiate a 'Paginator' object and set some attributes when we get different page number and return different arrange of content. It is available on some csdn blogs. Just searching for it.

Here, let's make it stupid simple.:)

Python code:

class LatestNew(generic.ListView):
    day = timezone.now() - timezone.timedelta(hours=24)
    template_name = "stockinfo/news.html"
    queryset = News.objects.filter(pub_date__gte=day).order_by("-pub_date")
    context_object_name = 'relate_news'
    paginate_by = 60    # for pagination, this is all 

 

Template html file.

......

......

{% include 'page_navigator.html' with pname='page' %}

 

 

Html page navigator block. Just '{% include "page_navigator.html"%}'.

{% if is_paginated %}
    <ul class="pagination">
        {% if page_obj.has_previous %}
            <li>
                <span><a href="?{{ pname }}={{ page_obj.previous_page_number }}">Previous</a></span>
            </li>
        {% endif %}
        <li class="">
            <span>Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.</span>
        </li>
        {% if page_obj.has_next %}
            <li>
                <span><a href="?{{ pname }}={{ page_obj.next_page_number }}">Next</a></span>
            </li>
        {% endif %}
    </ul>
{% else %}
    <h3>Your File Exam</h3>
    <p>File not yet available</p>
{% endif %}

 

 

 


Commenter Name
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
Commenter Name
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
Commenter Name
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
Search
Page view info
{'ip': '3.226.255.55', 'lat_lon': (41.890251, 12.492373), 'city': 'Rome', 'pv': 57344, 'cur_pv': 150, 'cur_daily_pv': 1, 'ur_pv': 3, 'ur_cur_all': 1, 'ur_today_all': 3, 'ur_today_cur': 1, 'daily_pv': 311, 'daily_ip': 83, 'whole_ip': 9757, 'ip_delta': 8}
AMap
Baidu Map