Answer the question
In order to leave comments, you need to log in
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)
from comments.models import Comment
comment = Comment.objects.all() # new add
context = {
"title": "List of Posts",
"object_list" : queryset,
"comment" : comment #new add
}
<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
{% for comment in instance.comment_set.all %}
{{ comment }}
{% endfor %}
{% for item in comment %}
{{ item }}
{% endfor %}
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)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question