2014-01-06 48 views
0

我可以在數據庫中添加評論,並在管理面板中看到它,但沒有看到在帖子(view_post.html)中添加評論。 我不明白原因django形式評論不可見

型號:

class Comment(models.Model): 
name = models.CharField('Имя:', max_length=100) 
create_date = models.DateField(blank=True, null=True) 
text = models.TextField() 

def __str__(self): 
    return '%s' % self.name 

形式:

class CommentForm(ModelForm): 
class Meta: 
    model = Comment 
    fields = ['name', 'create_date', 'text'] 

觀點:

def view_post(request, slug): 
post_detail = get_object_or_404(Article, slug=slug) 
form = CommentForm(request.POST or None) 
if form.is_valid(): 
    comment = form.save(commit=False) 
    comment.post_detail = post_detail 
    comment.save() 
    return redirect(request.path) 
return render_to_response('view_post.html', { 
    'post_detail': post_detail, 'form': form }, 
context_instance=RequestContext(request)) 

後的模板:

{% extends 'base.html' %} 
{% block head_title %}{{ post_detail.title }}{% endblock %} 
{% block title %}{{ post_detail.title }}{% endblock %} 
{% block content %} 
{{ post_detail.body }} 

{% if post_detail.comment_set.all %} 
    {% for comment in post_detail.comment_set.all %} 
     {{ comment.name }} 
     {{ comment.text }} 
    {% endfor %} 
{% endif %} 
<form action="" method="POST"> 
    {% csrf_token %} 
    <table> 
     {{ form.as_table }} 
    </table> 
<input type="submit" name="submit" value="Submit" /> 
</form> 
{% endblock %} 

回答

0

您在保存時將comment.post_detail設置爲當前文章,但在那裏您實際上似乎沒有post_detail ForeignKey。事實上,評論與文章之間或評論與任何內容之間似乎都沒有任何關係。

+0

感謝您的回覆,我瞭解問題的原因 – quest