简体   繁体   English

django 博客中的评论部分不会显示在每个单独的帖子下?

[英]Comment section in django blog won't show up under each individual post?

The comment successfully saves in the django admin but won't show up on the actual site.评论成功保存在 django 管理员中,但不会显示在实际站点上。

Here is the comment model:这是评论模型:

class comment(models.Model):
    linkedpost = models.ForeignKey(Post, related_name="postcomments", on_delete=models.CASCADE)
    commentauthor = models.ForeignKey(User, on_delete=models.CASCADE)
    body = models.TextField(max_length=100)
    date_posted = models.DateTimeField(default=timezone.now)

This the html code for the blog home.这是博客主页的 html 代码。 the post for loop goes through all the post objects and prints them out. post for 循环遍历所有 post 对象并将它们打印出来。 I created a comment loop to loop through all the comments for the linked post and print.我创建了一个评论循环来遍历链接帖子和打印的所有评论。 Is the problem in my html code?问题出在我的 html 代码中吗?

{% for post in posts %}
    <article class="media content-section">
        <img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}">
      <div class="media-body">
        <div class="article-metadata">
          <a class="mr-2" href="{% url 'user-posts' post.author.username %}">{{ post.author }}</a>
          <small class="text-muted">{{ post.date_posted|date:"F d, Y" }}</small>
        </div>
        <h2><a class="article-title" href="{% url 'post-detail' post.id %}">{{ post.title }}</a></h2>
        <p class="article-content">{{ post.content }}</p>
        <div>
            <h2>Comments</h2>
            {% for cmnts in linkedpost.postcomments %}
                #<a class="mr-2" href="{% url 'user-posts' cmnts.author.username %}">{{ cmnts.commentauthor }}</a>
                <small class="text-muted">{{ cmnts.date_posted|date:"F d, Y" }}</small>
                <p class="article-content">{{ cmnts.body }}</p>
            {% endfor %}
        </div>
      </div>
    </article>
{% endfor %}

The Post object is named post in the {% for post in posts %} loop, so you access the comments with:Post对象被命名post{% for post in posts %}循环,所以你访问的评论:

{% for cmnts in post.postcomments.all %}
    …
{% endfor %}

Note : Models in Django are written in PerlCase , not snake_case , so you might want to rename the model from comment to Comment .注意:Django 中的模型是用PerlCase编写的,而不是snake_case ,因此您可能希望将模型从comment重命名为Comment


Note : It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly.注意:通常最好使用settings.AUTH_USER_MODEL [Django-doc]来引用用户模型,而不是直接使用User模型 [Django-doc] For more information you can see the referencing the User model section of the documentation .有关更多信息,您可以查看文档引用User模型部分


Note : Django's DateTimeField [Django-doc] has a auto_now_add=… parameter [Django-doc] to work with timestamps.注意:Django 的DateTimeField [Django-doc]有一个auto_now_add=…参数 [Django-doc]来处理时间戳。 This will automatically assign the current datetime when creating the object, and mark it as non-editable ( editable=False ), such that it does not appear in ModelForm s by default.这将在创建对象时自动分配当前日期时间,并将其标记为不可编辑( editable=False ),这样默认情况下它不会出现在ModelForm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM