在django模板中对div进行排序

Sort divs in django template

我是 Django 的新手,请帮助解决我的问题。

class Article(models.Model):
articlecategory = models.ForeignKey(ArticleCategory)

文章类别可以按Big_news(连续1条新闻)或Small_news(连续3条新闻)

我想要模板中的逻辑:

如何在 Django 中获取这个(第 3 段)?

您应该在您的视图中处理此逻辑。它不属于您的模板。

class ArticleRowViewModel:
    def __init__(self, articles):
        self.articles = articles

    def is_big_news(self):
        return len(self.articles) == 1

articles = Article.objects.all().order_by('-date')
previous_articles = []
view_models = []
for article in articles:
    if article.big_news:
        # TODO: I don't know if this is right.
        # Your business logic didn't cover it. I'm treating it if there
        # any small news articles, that they should appear as such before
        # the current big `article` in the loop.
        if previous_articles:
            view_models.append(ArticleRowViewModel(previous_articles))
            previous_articles = []
        view_models.append(ArticleRowViewModel([article]))
    else:
        previous_articles.append(article)
        # Check if we have 3 small articles.
        if len(previous_articles) == 3:
            view_models.append(ArticleRowViewModel(previous_articles))
            previous_articles = []  
render_or_something(view_models)