2014-01-06 50 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