如何在每个第三个帖子中放一个横幅
def all_ads(request):
all_ads = CreateNewAdvert.objects.filter(is_active=True)
all_ads_list = list()
banner = Banners.objects.all()
counter = 0
for i in all_ads:
if len(all_ads_list) % 3 == 0:
all_ads_list.append(i)
all_ads_list = list()
all_ads_list.append(banner[counter])
print i
paginator = Paginator(all_ads, 10) # Show 25 contacts per page
page = request.GET.get('page')
try:
all_ads_paginated = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
all_ads_paginated = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
all_ads_paginated = paginator.page(paginator.num_pages)
context = {
'all_ads': all_ads_paginated,
}
return context
需要得到这个: 帖子1, 帖子2, 帖子3, 横幅1,后4, 帖子5, 帖子6, 横幅2
答案 0 :(得分:0)
def all_ads(request):
all_ads = CreateNewAdvert.objects.filter(is_active=True)
paginator = Paginator(sort_adds(all_ads), 10) # Show 25 contacts per page
page = request.GET.get('page')
try:
all_ads_paginated = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
all_ads_paginated = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
all_ads_paginated = paginator.page(paginator.num_pages)
context = {
'all_ads': all_ads_paginated,
}
return context
结果
def sort_adds(obj):
sell_adds_list = list()
banner = Banners.objects.all()
counter = 0
adds_counter = 0
for i in obj:
sell_adds_list.append(i)
adds_counter += 1
if adds_counter % 3 == 0:
if not counter > len(banner):
sell_adds_list.append(banner[counter])
counter += 1
else:
pass
return sell_adds_list