1

我想要做的是只有当前建议的评论出现在详细信息页面上。但是我在 VIEWS 文件中编码的方式会出现所有评论。这是我的看法:

def detail(request, suggestion_id):
    suggestion = get_object_or_404(Suggestion, pk=suggestion_id)
    comments = get_object_or_404(Comments.objects.all(), pk=suggestion_id) #This is my question
    if request.method == 'POST':
        commentform = CommentForm(request.POST)
        if commentform.is_valid():
            cd = commentform.cleaned_data
            comment = Comments.objects.get(sid=sid) 
            comment.comment = cd.get('comment')
            comment.save()
    commentform=CommentForm()
    context = {
        'suggestion' : suggestion,
        'commentform' : commentform,
        'comments' : comments,
    }

    return render(request, 'suggestionbox/detail.html', context)

楷模:

class Suggestion(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    description = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True)
    status = models.ForeignKey('Status')
    category = models.ForeignKey('Category')

    def __unicode__(self):
        return self.title

class Comments(models.Model):
    comment = models.TextField()
    user = models.ForeignKey(User)
    pub_date = models.DateTimeField(auto_now_add=True)
    sid = models.ForeignKey(Suggestion) # Suggestion ID

    def __unicode__(self):
        return self.comment[:50]

我是 Django 的新手,我仍在试图弄清楚它是如何工作的。任何帮助,将不胜感激。

4

0 回答 0