繁体   English   中英

/products/ object 的 TypeError 类型“ModelBase”没有 len()

[英]TypeError at /products/ object of type 'ModelBase' has no len()

所以我尝试使用分页,当我访问页面时,我在 /products/ object 的“ModelBase”类型中得到 TypeError 没有 len()。 回溯显示此行有错误books = haha.page(page)

视图.py

def showproducts(request):
    oof = CartItem.objects.filter(user=request.user).values_list('book', flat=True)
    lmao = OrderItem.objects.filter(user=request.user).values_list('book', flat=True)
    hehe = CartItem.objects.filter(user=request.user)
    category = Category.objects.all()
    haha = Paginator(Book, 2)
    page = request.GET.get('page')
    if page is None:
        page = 1
    fianlprice = 0
    for item in hehe:
        fianlprice += item.book.price
    # books = Book.objects.all()
    books = haha.page(page)
    return render(request, 'main/products.html', {'books':books, 'price':fianlprice, 'cart':oof, 'order':lmao, 'category':category})

产品.html

<h1>Products</h1>
<h1>{{ error }}</h1>
{% if user.is_authenticated %}
<h1>Your cart currently costs ${{ price }}</h1>
{% else %}
<h1>Please login to view your cart</h1>
{% endif %}
<form method="GET" action="/search/">

    <label>Choose a category</label>

<select name="category" id="category">
    <option value="All" selected>All</option>
    {% for name in category %}
  <option value="{{ name.name }}">{{ name.name }}</option>
    {% endfor %}
</select>

    <input type="text" placeholder="Search here" name="search" id="search">
    <button type="submit">Search</button>
</form>
{% for book in books %}
<a href="{% url 'detailview' book.id %}"><h3>{{ book.name }}</h3></a>
<img src= "/media/{{ book.image }}" alt="">
<p>{{ book.description }}</p>
        {% if not user.is_authenticated %}
        <p>Please login</p>
        {% else %}
        {% if book.id in cart %}
        <form method="POST" action="/removefromcartforhome/">
            {% csrf_token %}
            <button type="submit" name="removeid" value="{{ book.id }}">remove item from cart</button>
        </form>
        {% elif book.id in order %}
        <h3>You already own this</h3>
        {% else %}
        <form method="POST" action="/addtocartforhome/">
            {% csrf_token %}
            <button type="submit" name="bookid" value="{{ book.id }}">Add to cart</button>
        </form>
    {% endif %}
        {% endif %}
{% endfor %}

{% if books.has_other_pages %}

    {% if listing.has_previous %}
    <li class="page-item">
        <a href="?page={{book.previous_page_number}}" class="page-link">&laquo;</a>
    </li>
    {% else %}
    <li class="page-item disabled">
        <a class="page-link">&laquo;</a>
    </li>
    {% endif %}
    {% for i in books.paginator.page_range %}
        {% if books.number == i %}
        <li class="page-item active">
            <a class="page-link">{{i}}</a>
        </li>
        {% else %}
        <li class="page-item">
            <a href="?page={{i}}" class="page-link">{{i}}</a>
        </li>
        {% endif %}
    {% endfor %}
    {% if books.has_next %}
        <li class="page-item">
            <a href="?page={{books.next_page_number}}" class="page-link">&raquo;</a>
        </li>
    {% else %}
        <li class="page-item disabled">
            <a class="page-link">&raquo;</a>
        </li>
    {% endif %}

{% else %}


{% endif %}

分页器需要一个QuerySet ,而不是 model,所以:

#               queryset ↓
haha = Paginator(Book.objects.all(), 2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM