分页代码:
以下是我的分页打印
但是我遇到了一个错误:
分页代码:
try:
page = paginator.page(page_number)
print(page.object_list)
以下是我的分页打印
print(page.object_list)
的输出结果:<QuerySet [<Comment: I can't believe it's happeinig>, <Comment: Just trying to fill up the comments>, <Comment: Another one!>, <Comment: Evenmore noe>, <Comment: Something>, <Comment: Lol>, <Comment: Are comments showing up?>, <Comment: Great for the economy.>, <Comment: honestly>, <Comment: Even though the the onlyEven though the only one to udnertstnaf!>]>
<QuerySet [<Comment: Yeah it's crazy how fast aswell. It's very awesome how it's doing atm. >]>
<QuerySet []>
<QuerySet [<Comment: Sure>, <Comment: No worries>]>
<QuerySet []>
<QuerySet []>
<QuerySet [<Comment: attempt 2!>]>
<QuerySet [<Comment: Attempt 3!>]>
<QuerySet []>
<QuerySet [<Comment: 12>]>
<QuerySet []>
<QuerySet [<Comment: Somewhere?>]>
<QuerySet []>
<QuerySet [<Comment: lol>]>
<QuerySet []>
<QuerySet [<Comment: 12>]>
<QuerySet []>
正如您所看到的,我有一些空的查询集,这些导致我的代码出现错误。因此,我想遍历这些查询集并找出其中的空集。我尝试添加了以下for循环:
for i in page.object_list:
if len(i) < 0:
但是我遇到了一个错误:
TypeError at /news/11/
object of type 'Comment' has no len()
需要帮助请告知。
尝试删除一个查询集(queryset),如果它是空的:
try:
page = paginator.page(page_number)
if page.object_list:
pass
else:
page.delete()
错误:
AttributeError at /news/11/
'CustomPage' object has no attribute 'delete'