django表单注释不可见
我可以在db中添加评论,并在管理面板中查看评论,但在帖子中看不到添加的评论(view_post.html)。 我不明白为什么会这样 型号:django表单注释不可见,django,django-forms,Django,Django Forms,我可以在db中添加评论,并在管理面板中查看评论,但在帖子中看不到添加的评论(view_post.html)。 我不明白为什么会这样 型号: class Comment(models.Model): name = models.CharField('Имя:', max_length=100) create_date = models.DateField(blank=True, null=True) text = models.TextField() def __str__(self):
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 %}
{%extends'base.html%}
{%block head\u title%}{{post\u detail.title}}{%endblock%}
{%block title%}{{post_detail.title}{%endblock%}
{%block content%}
{{post_detail.body}
{%if post_detail.comment_set.all%}
{post_detail.comment_set.all%}
{{comment.name}
{{comment.text}
{%endfor%}
{%endif%}
{%csrf_令牌%}
{{form.as_table}}
{%endblock%}
您在保存时设置了注释。在当前文章中发布详细信息,但实际上您似乎没有发布详细信息
外键。事实上,你的评论和文章之间,或者评论和任何东西之间,似乎都没有任何关系。多亏了你的回复,我明白了问题的原因