【发布时间】:2016-03-19 00:16:49
【问题描述】:
我有一个formView 课程,如下所示:-
view.py
class ThreadForm(FormView):
template_name = 'thread.html'
form_class = ThreadModelForm
success_url = '/success'
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
print form.cleaned_data
return super(ThreadForm, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(ThreadForm, self).get_context_data(**kwargs)
context['second_form'] = MessageModelForm
return context
thread.html
{form.as_p}
{second_form.as_p}
SUBMIT
在我的模板thread.html 中,我有两个模型表单,但只有一个提交按钮。问题是我没有从我的second_form 获得任何数据,也无法验证second_form。我从form 接收数据,但不是从second_form 接收数据。谁能告诉我如何验证second_form 数据。谢谢
一种方法是使用request.post['data'],但还有其他方法吗?
【问题讨论】:
-
恕我直言,在这种情况下最好使用模板视图。 FormView 旨在仅处理一种表单
-
你能给我举个模板视图处理多个表单的例子吗?
标签: django django-models django-templates django-views django-class-based-views