django显示帖子和评论

时间:2019-02-09 08:35:19

标签: python django

这是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,一些教程页面上找到答案。...

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

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

1 个答案:

答案 0 :(得分: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 %}