X
X
xxx2017-02-26 21:50:57
Django
xxx, 2017-02-26 21:50:57

Displaying fields from another django app?

Wanted to add comments to the blog.
1. Created a new application comments
2. model:

from __future__ import unicode_literals
from django.db import models
from django.conf import settings
from blog.models import Post
# Create your models here.

class Comment(models.Model):
  user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
  post = models.ForeignKey(Post)
  content = models.TextField()
  time = models.DateTimeField(auto_now_add = True)

  def __unicode__(self):
    return str(self.user.username)

3. Made migrations
4. Made imports into the blog application view 5. Added to the view that displays the post
from comments.models import Comment
comment = Comment.objects.all() # new add
context = {
  "title": "List of Posts",
  "object_list" : queryset,
  "comment" : comment  #new add
  }

6. Added to the template under the post Nothing is displayed, but if I add I get How to display data from the Comment model of a new application in the views of another?
<div>{{ comment.user }}</div>
<div>{{ instance.comment_set.all }}</div>
<QuerySet [<Comment: User1>]>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artyom Bryukhanov, 2017-02-26
@Heavy10110

{% for comment in instance.comment_set.all %}
  {{ comment }}
{% endfor %}

or so, it's not entirely clear from your context
{% for item in comment %}
  {{ item }}
{% endfor %}

A
Alexey Sergeev, 2017-02-27
@SergeevAI

class Comment(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    post = models.ForeignKey(Post, related_name="comments")
    content = models.TextField()
    time = models.DateTimeField(auto_now_add = True)

Remove comment = Comment.objects.all() and "comment" : comment
and in the template write {% for comment in post.comments %}
{{ comment.content }}
Something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question