【问题标题】:Putting two different variables from two different models at the same line in Django templates将来自两个不同模型的两个不同变量放在 Django 模板的同一行
【发布时间】:2020-10-25 09:40:51
【问题描述】:

我需要一个类似的 Django 模板

“第一个博客标题”-(“第一个博客的评论数”)

“第二个博客标题”-(“第二个博客的评论数”)

最简单的方法是什么? 我试图在我的views.py 中创建一个上下文,它同时来自博客模型和评论模型,因为变量属于不同的模型。然后我需要将两个变量都放入我的 html 中的 for 循环中,但结果是一个嵌套循环。所以我没有得到我想要的样子。

我对各种解决方案持开放态度,不仅来自后端,还来自前端
谢谢

我更新了我的问题并添加了我的模型和视图

class Author(models.Model):
    name = models.CharField(max_length=100)
    surname = models.CharField(max_length=100)
    email = models.EmailField()
    def __str__(self):
        self.name
    
class Blog(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    heading = models.CharField(max_length= 255)
    text = models.CharField(max_length= 5000)
    
     def __str__(self):
        self.name
    
class Comment(models.Model):
    blog = models.ForeignKey(Blog, on_delete=models.CASCADE, related_name='comments')
    author = models.CharField(max_length=100)
    text_comment = models.CharField(max_length=2000)



def index(request):
    blogs = Blog.objects.all
    comments = Blog.objects.annotate(number=Count('comments'))
    context = {'blog':blogs, 'comments':comments}
    return render(request, 'blog/index.html', context)

模板(不起作用)

{% for blog in blogs %}
{% for comment in comments %}
{{ blog.heading }} {{ comment.number }}
{% endfor %}
{% endfor %}

非常感谢

【问题讨论】:

  • 博客和cmets有关系吗?你能分享你的models.py吗?

标签: django django-models django-templates


【解决方案1】:

很难想象你有什么数据。

例如,最后你在视图的返回语句之前得到两个字典 blogscomments
之后,您需要通过外键加入两个字典。 comment["blog_id"]blog["id"] 在我的示例中。

例如:

blogs = [
 {"name": "A", "id": 1, "text": "someblogA"},
 {"name": "B", "id": 2, "text": "someblogB"},
 {"name": "C", "id": 3, "text": "someblogC"},
 {"name": "D", "id": 4, "text": "someblogD"},
]

comments = [
 {"text": "commentA", "id": 11, "blog_id": 1},
 {"text": "commentB", "id": 12, "blog_id": 2},
 {"text": "commentC", "id": 13, "blog_id": 2},
 {"text": "commentD", "id": 14, "blog_id": 3},
]

for blog in blogs:
  blog['num_of_comments'] = 0
  for comment in comments:
    if comment["blog_id"] == blog["id"]:
      blog['num_of_comments'] += 1

之后你就可以使用这个语法了:

{% for blog in blogs %}
  {{blog.name }} {{blog.num_of_comments }}
{% endfor %}

但在我看来,更好的方法是创建 Serializer 并在其中加入博客和 cmets。
Serializer relations with django rest framework

【讨论】:

    【解决方案2】:

    由于您已经注释了每个博客的 cmets 计数,您可以从 Blog 查询集中获得“数字”:

    # views.py
    def index(request):
        blog_with_comments = Blog.objects.annotate(number=Count('comments'))
        context = {'blog_with_comments': blog_with_comments}
        return render(request, 'blog/index.html', context)
    
    #template
    {% for blog in blog_with_comments %}
       {{ blog.heading }} {{ blog.number }}
    {% endfor %}
    

    【讨论】:

    • 不客气。如果答案足够,你能解决问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 2022-01-22
    • 2014-05-09
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    相关资源
    最近更新 更多