django显示帖子和评论

问题描述 投票:1回答:1

这是models.py

class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=100)
    content = models.TextField()
    date = models.DateTimeField(default=timezone.now)

    def write(self):
        self.date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

class Comment(models.Model):
    post = models.ForeignKey('humorge.post', on_delete=models.CASCADE, related_name='comments')
    author = models.CharField(max_length=100)
    content = models.TextField()
    date = models.DateTimeField(default=timezone.now)

    def add_coment(self):
        self.date = timezone.now()
        self.save()

    def __str__(self):
        return self.content

这是views.py

def mainpage(request):
    return render(request, 'index.html')

def post(request):
    datas = Post.objects.order_by('-date')
    comments = Comment.objects.all()
    return render(request, 'post.html', {'datas': datas, 'comments': comments})

这是模板

{% extends 'layout.html' %}

{% block main %}
<div class="container">
  {% for data in datas %}
    <div class="row">
      <div class="col s12 m7">
        <div class="card">
          <div class="card-image">
            <img src="#">
            <span class="card-title">{{ data.title }}</span>
          </div>
          <div class="card-content">
            <p>{{ data.content }}</p>
          </div>
          <div class="card-action">
            <p>{{ data.date }}</p>
          </div>
          <div class="card-action">
            {% for comment in comments %}
              {% if comment.post == data.title %}
                {{ comment.date }}<br>
                <strong>{{ comment.author }}</strong>
                <p>{{ comment.content }}</p>
              {% endif %}
            {% empty %}
              <p>There is no comments</p>
            {% endfor %}
          </div>
        </div>
      </div>
    </div>
  {% empty %}
    <p>there is no posts</p>
  {% endfor %}
</div>
{% endblock %}

我要显示帖子和评论

[当我尝试有两个帖子并且每个帖子都有一个评论时

它显示每个帖子都有两个评论

所以我在模板中添加了{% if comment.post == data.title %}

但它什么也没显示

我试图在google,youtube,一些教程页面上找到答案...。

但是我发现只是如何在帖子中添加评论。。

或显示帖子和评论,但实际上是一篇帖子一篇评论

python django
1个回答
1
投票

您不需要在视图中查询评论列表,只需要发布:

def post(request):
    datas = Post.objects.order_by('-date')
    return render(request, 'post.html', {'datas': datas})

然后您可以在模板中执行以下操作:

{% block main %}
<div class="container">
  {% for data in datas %}
    ...
      <div class="card-action">
        {% for comment in data.comments.all %}  // Gives list of all comments for the current post ie. data
            {{ comment.date }}<br>
            <strong>{{ comment.author }}</strong>
            <p>{{ comment.content }}</p>
        {% empty %}
          <p>There is no comments</p>
        {% endfor %}
      </div>
      {% empty %}
      <p>there is no posts</p>
    {% endfor %}
  </div>
  ...
{% endblock %}

顺便说一句,模板中if条件的问题是您正在执行{% if comment.post == data.title %},但是commpent.post是模型Post的对象,而post.title是字符串,因此它们永远不相等。您需要做的是{% if comment.post == data %}{% if comment.post.title == data.title %}

© www.soinside.com 2019 - 2024. All rights reserved.