Answer the question
In order to leave comments, you need to log in
How to make an object of another model iterate after 2 iterations of the first, etc.?
How to make it so that, say, every two posts (Post - a separate model) only one banner appears (Banner - a separate model) and then again two posts and a banner, and so on.
Here is an example output diagram:
post1, post2
banner1
post3, post4
banner4
Answer the question
In order to leave comments, you need to log in
For example, like this:
banners = ['banner1', 'banner2', 'banner3']
posts = ['post1', 'post2', 'post3', 'post4', 'post5', 'post6', 'post7', 'post8']
banner_step = 2
for index, post in enumerate(posts):
print(post)
if (index+1) % banner_step == 0 and banners:
print(banners.pop(0))
post1
post2
banner1
post3
post4
banner2
post5
post6
banner3
post7
post8
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question