Django Paginator给出错误__init __(),缺少两个必需的位置参数:'number'和'paginator'

时间:2019-03-30 14:45:38

标签: python django django-pagination

我使用了Django文档中的以下代码,由于某种原因该代码无法正常工作

<div class="pagination">
 <span class="step-links">
        {% if productsList.has_previous %}
            <a href="?page=1">&laquo; first</a>
            <a href="?page={{ products.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ productsList.number }} of {{ productsList.paginator.num_pages }}.
        </span>

        {% if productsList.has_next %}
            <a href="?page={{ productsList.next_page_number }}">next</a>
            <a href="?page={{ productsList.paginator.num_pages }}">last &raquo;</a>
        {% endif %}
    </span>
</div>

当我进入页面时出现错误:

__init__() missing 2 required positional arguments: 'number' and 'paginator'

奇怪! 我也尝试过使用page参数传递url,但仍然无法正常工作!

def products(request):
    products_first_ten = Products.objects.order_by('productid')[:100]    
    products_List = []
    for product in products_first_ten:
        book = Books.objects.get(productid = product.productid)
        products_List.append(book)      

    paginator = Paginator(products_List, 10) # Show 25 contacts per page
    page = request.GET.get('page', 1)
    try:
        products_List = paginator.page(page)
    except PageNotAnInteger:
        products_List = paginator.page(1)
    except EmptyPage:
        products_List = paginator.page(paginator.num_pages)

    return render(
        request, 'catalog/products.html',
        context = 
        {
            'productsList' : products_List
        }
    )

0 个答案:

没有答案