【发布时间】:2015-12-24 17:19:43
【问题描述】:
我使用基于 Django 的视图。我有一个显示对象的类(DetailView),现在我想在同一页面中添加一个表单,就像在 DetailView 中一样。
我的意见.py:
class CommentFormView(FormView):
form_class = AddCommentForm
success_url = '/'
class BlogFullPostView(CommentFormView, DetailView):
model = Post
template_name = 'full_post.html'
pk_url_kwarg = 'post_id'
context_object_name = 'post'
def get_context_data(self, **kwargs):
context = super(BlogFullPostView, self).get_context_data(**kwargs)
context['comments'] = Comment.objects.filter(post=self.object)
return context
也许,你明白 - BlogFullPostView - 显示页面,我想在其中添加表单。 CommentFormView - 查看评论。
我的表格:
class AddCommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('content',)
widgets = {
'content': forms.TextInput(attrs={
'class': 'form-control'
})
}
labels = {
'content': 'Content'
}
def __init__(self, *args, **kwargs):
super(AddCommentForm, self).__init__(*args, **kwargs)
所以,在模板中,我尝试添加表单:
<form method="post" action="" role="form">
{{ form }}
</form>
它什么也不显示:(
我该怎么办?
【问题讨论】:
-
你能检查你的 html 看看是否有
<form method="post" action="" role="form">吗? -
是的!它在那里:
<form method="post" action="" role="form"> {{ form }} </form>
标签: python django django-class-based-views