我有3个查询集:Post
,Two
和AdvertisePost
。
目前的工作方式是:我的网站被分页,每页都有14个对象。在这14个对象中,它是这样订购的:
1. Post
2. Post
3. Post
4. Post
5. Two
6. Post
7. Post
8. Post
9. Post
10.Two
11.Post
12.Post
13.Post
14.Post
但我现在希望在每个页面的第12个插槽中都有一个AdvertisePost
。不要替换第12个插槽中的Post
,而是将其推下。但是不要把它压低,以免它破坏下一页的形成。
这是我目前的代码:
two_list = []
returned_list = []
two = Two.objects.all()
for i in two:
two_list.append(i)
ads = AdvertisePost.objects.all()
posts = Post.objects.all()
for n, post in enumerate(posts):
returned_list.append(post)
if len(two_list) > 0 and (n + 1) % 4 == 0 and len(returned_list) % 14 != 0:
returned_list.append(two_list.pop(0))
paginator = Paginator(returned_list, 14)
page = request.GET.get('page')
try:
lst = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
lst = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
lst = paginator.page(paginator.num_pages)
我知道如何在每个页面的每第12个插槽中放置一个AdvertisePost
对象吗?
修改
以下代码以5个帖子而不是4个帖子开始。之后,编队工作正常(帖子分批为4个)。
for number in range(all_count):
if number % 5 == 0 and len(two_list) and number != 0:
answer.append(two_list.pop())
elif number % 12 == 0 and len(ads) and number != 0:
answer.append(ads.pop())
elif len(posts):
answer.append(posts.pop())