Django Paginator - 我在继承自 Paginator 的 class 中创建了一个函数,以及为什么我不能在前端使用这个函数
Django Paginator - I create a function in a class inherting from Paginator and why I can't use this function in front-end
我是Django的新手,我想在前端使用pager_num_range来显示页码,但是前端无法识别pager_num_range,我不知道为什么。
后端代码(我编写这些是为了学习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})
前端代码:
<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>
这是 urls.py 文件:
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), ]
试试这个如果你添加一个新方法或覆盖一个方法意味着应该return根据源代码的有效值
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})
在html中使用这个
<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>
在网址中
url(r'^$', views.index, name='index'),
我是Django的新手,我想在前端使用pager_num_range来显示页码,但是前端无法识别pager_num_range,我不知道为什么。
后端代码(我编写这些是为了学习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})
前端代码:
<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>
这是 urls.py 文件:
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), ]
试试这个如果你添加一个新方法或覆盖一个方法意味着应该return根据源代码的有效值
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})
在html中使用这个
<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>
在网址中
url(r'^$', views.index, name='index'),