【问题标题】:Pagination in Wagtail with {%for %} cycle使用 {%for %} 循环在 Wagtail 中进行分页
【发布时间】:2020-07-18 19:46:45
【问题描述】:

我在理解 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 %}

因为我买了所有的产品。但是我该如何解决呢?

【问题讨论】:

    标签: django python-3.x wagtail


    【解决方案1】:

    我不确定您的上下文变量在您的模板中是否正确。 您可以尝试获取所有产品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 %}

    【讨论】:

      猜你喜欢
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 2021-07-27
      相关资源
      最近更新 更多