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

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

或显示帖子和评论,但实际上只有一篇帖子和一条评论

维拜·维沙尔

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

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 %}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章