我在Wagtail cms中了解Django分页有一些问题。我阅读了此主题Pagination in Wagtail
所以这对我来说不太好。在我的获取产品模板中,我使用此代码
{% for product in page.get_children.specific %}
<div class="col-12 col-sm-6 col-lg-4">
<div class="single-best-receipe-area mb-30">
{% image product.product_photo width-400 %}
<div class="receipe-content">
<a href="{% pageurl product %}">
<h5>{{ product.title }}</h5>
</a>
</div>
</div>
</div>
{% endfor %}
然后:
<ul class="pagination">
{% if product.has_previous %}
<li><a href="?page={{ product.previous_page_number }}"><i class="fa fa-angle-left"></i></a></li>
{% endif %}
{% for page_num in product.paginator.page_range %}
<li {% if page_num == product.number %}class="active" {% endif %}><a
href="?page={{ page_num }}">{{ page_num }}</a></li>
{% endfor %}
{% if product.has_next %}
<li><a href="?page={{ product.next_page_number }}"><i class="fa fa-angle-right"></i></a></li>
{% endif %}
</ul>
在我的models.py中,我使用此:
class ProductsPage(Page):
body = models.CharField(max_length=255, blank=True, help_text = 'Описание страницы')
product_image = models.ForeignKey(
"wagtailimages.Image",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
help_text='Фотография Категории'
)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
ImageChooserPanel('product_image'),
]
def get_context(self, request):
context = super(ProductsPage, self).get_context(request)
all_product = OneProduct.objects.live()
paginator = Paginator(all_product, 1) # Show 3 product per page
page = request.GET.get('page')
try:
product = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
product = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
product = paginator.page(paginator.num_pages)
# make the variable 'product' available on the template
context['product'] = product
return context
最后我看到了分页,但是每一页上都有所有产品我了解问题出在模板的这一部分
{% for product in page.get_children.specific %}
因为我购买了所有产品。但是我该如何解决呢?
我不确定您的上下文变量在模板中是否正确。您可能会尝试获取所有产品all_products = OneProduct.objects.all()
不是最好的,但分页的有效示例。它可能会帮助您。
paginator = Paginator(data, amount) # amount of products per page
page_number = request.GET.get('page', 1)
page = paginator.page(page_number)
is_paginated = page.has_other_pages()
if page.has_previous():
prev_url = '?page={}'.format(page.previous_page_number())
else:
prev_url = ''
if page.has_next():
next_url = '?page={}'.format(page.next_page_number())
else:
next_url = ''
last_url = '?page={}'.format(page.paginator.page_range[-1])
并将page
传递到上下文变量context ={'page_object': page}
。
因此您可以将其用于周期{% for product in page_object.object_list %}