python - 'ModelBase' 类型的 /products/ 对象的 TypeError 没有 len()
问题描述
所以我尝试使用分页,当我访问页面时,我在 /products/ '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">«</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">«</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">»</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">»</a>
</li>
{% endif %}
{% else %}
{% endif %}
解决方案
分页器需要一个QuerySet
,而不是模型,所以:
# queryset ↓
haha = Paginator(Book.objects.all(), 2)
推荐阅读
- android - 如何使用 HttpURLConnection 在 android 中使用 POST 方法上传数据?
- aws-lambda - 在 ECR PutImage 事件上触发 lambda
- javascript - 用json中的子节点替换父节点
- crystal-lang - 为了方便起见,我可以在 Crystal 中命名一个类型吗?
- javascript - 用户名必须包含以下条件
- mysql - MySQL 大删除
- java - 如何显示 on 开关的非活动状态
- vba - 单击单元格的 Excel 宏取消隐藏下面的行
- java-8 - Java 8 apply list of functions on list of values
- java - LocalDateTime 通过 StoredProcedureQuery 变成 bytea