简体   繁体   中英

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

So I'm trying to use pagination, When I visit the page I get TypeError at /products/ object of type 'ModelBase' has no len(). The traceback shows this line has the error books = haha.page(page)

views.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})

products.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 %}

The paginator needs a QuerySet , not a model, so:

#               queryset ↓
haha = Paginator(Book, 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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