When you try to display a field in a template, the necessary information is not displayed. Models:

class Comment(models.Model): text = models.TextField() published_date = models.DateTimeField( blank=True, null=True) post = models.ForeignKey('Post') author = models.ForeignKey('auth.User') class Post(models.Model): author = models.ForeignKey('auth.User') title = models.CharField(max_length=200) text = models.TextField() 

Part of the post-class post-object templat. Added Comment to this object in the admin panel

 <div class="comment"><h1>{{ post.comment.text}}</h1></div> 
  • one
    read about the related_name attribute for link fields - waynee

2 answers 2

Since you have one post and many comments, you can’t directly turn to the text of the comment (you understand that the first, but obviously it is not specified), only to the list of comments:

  {% for comment in post.comment_set.all %} <div class="comment"><h1>{{ comment.text}}</h1></div> {% endfor %} 

dock

    Newbies often get confused. The ForeignKey field defines the relation of many_to one, and not one_of_ many. That is, taking into account the code you have written, there will be a lot of Comment from one Post . This means that access looks like:

     <div class="comment"><h1>{{ comment.post.text}}</h1></div>