简体   繁体   中英

Django Paginator - I create a function in a class inherting from Paginator and why I can't use this function in front-end

I'm a newbie to Django, I would like to use pager_num_range in front end for displaying the page number,but front end can't recognize pager_num_range, I don't know why.

back-end code(I program these come for learning Paginator) :

USER_LIST = []
for i in range(1, 999):
    temp = {'name': 'root' + str(i), 'age': i}
    USER_LIST.append(temp)

class CustomPaginator(Paginator):
def __init__(self,*args,**kwargs):
    super(CustomPaginator, self).__init__(*args, **kwargs)

def pager_num_range(self):
    return range(1,2)


def index1(request):
    current_page = request.GET.get('p')
    paginator = Paginator(USER_LIST, 10)
    try:
        posts = paginator.page(current_page)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)

return render(request,'index1.html',{'posts':posts})

front-end code:

<ul>
    {% for row in posts.object_list %}
        <li>{{ row.name }}-{{ row.age }}</li>
    {% endfor %}
</ul>

{% if posts.has_previous %}
    <a href="/index1.html?p={{ posts.previous_page_number }}">Last Page</a>
    {% else %}
    <a href="#">Next Page</a>
{% endif %}

{% for i in posts.paginator.pager_num_range %}
{% endfor %}

{% if posts.has_next %}
<a href="/index1.html?p={{ posts.next_page_number }}">Next Page</a>
{% endif %}

<span>
    {{ posts.number }}/{{ posts.paginator.num_pages }}
</span>

</body>

this is urls.py file:

urlpatterns = [
    path('admin/', admin.site.urls),

    path('index',views.index),
    path('index1',views.index1),
]

urlpatterns = [ path('admin/', admin.site.urls), 
                path('index.html',views.index), 
                path('index1.html',views.index1), ]

try this if your adding a new method or overriding a methods means should return the valid value according to souce code

    class CustomPaginator(Paginator):
    def __init__(self,*args,**kwargs):
        super(CustomPaginator, self).__init__(*args, **kwargs)

    @property
    def pager_num_range(self):
        """
        Return a 1-based range of pages for iterating through within
        a template for loop.
        """
        return range(1, self.num_pages + 1)


def index(request):
    USER_LIST = []
    for i in range(1, 53):
        temp = {'name': 'root' + str(i), 'age': i}
        USER_LIST.append(temp)
    current_page = request.GET.get('p')
    paginator = CustomPaginator(USER_LIST, 10)
    try:
        posts = paginator.page(current_page)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)

    return render(request,'polls/index.html',{'posts':posts})

in html use this

<ul>
    {% for row in posts.object_list %}
        <li>{{ row.name }}-{{ row.age }}</li>
    {% endfor %}
</ul>

{% if posts.has_previous %}
    <a href="/?p={{ posts.previous_page_number }}">Last Page</a>
    {% else %}
    <a href="#">Next Page</a>
{% endif %}

{% for i in posts.paginator.pager_num_range %}
{% if posts.number == i %}
<li class="active"><span>{{ i }} <span>(current)</span></span></li>
{% else %}
<li><a href="?p={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}

{% if posts.has_next %}
<a href="/?p={{ posts.next_page_number }}">Next Page</a>
{% endif %}

<span>
    {{ posts.number }}/{{ posts.paginator.num_pages }}
</span>

in urls

url(r'^$', views.index, name='index'),

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