2013-05-03 59 views
0
class Post(models.Model): 
    title = models.CharField(max_length=255) 
    category = models.ForeignKey(Category) 
    ... 

class Comment(models.Model): 
    body = models.TextField() 
    post = models.ForeignKey(Post) 
    ... 

意見如何從模板中的單個帖子獲取評論?

def single_post(request,slug): 
    p = Post.objects.get(slug=slug) 
    cat = Category.objects.all() 
    return render_to_response('single_post.html',{'p':p,'cat':cat},context_instance=RequestContext(request)) 

如何從模板單個帖子的評論?

回答

1

在訪問量:

def single_post(request,slug): 
    p = Post.objects.get(slug=slug) 
    cat = Category.objects.all() 
    comments = Comment.objects.filter(post=post) 
    return render_to_response('single_post.html',{'p':p,'cat':cat, 'comments':comments},context_instance=RequestContext(request)) 

在模板中,是這樣的:

{% for comment in comments %} 
    {{ comment.body}} 
{% endfor %}