bmi*_*kie 8 forms django class
我一直在寻找如何使用较新的基于Django类的视图方法在一个页面上显示2个独特的表单.
任何人都可以参考吗?或者提供一个基本的例子.谷歌不是我的"朋友".
关键是你甚至不必使用其中一个FormView子类来处理表单.您只需手动添加用于处理表单的机器.在使用FormView子类的情况下,它只处理1个而且只处理1个表单.因此,如果您需要两个表单,则只需手动处理第二个表单.我DetailView用作基类只是为了表明你甚至不必从FormView类型继承.
class ManualFormView(DetailView):
def get(self, request, *args, **kwargs):
self.other_form = MyOtherForm()
return super(ManualFormView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.other_form = MyOtherForm(request.POST)
if self.other_form.is_valid():
self.other_form.save() # or whatever
return HttpResponseRedirect('/some/other/view/')
else:
return super(ManualFormView, self).post(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(ManualFormView, self).get_context_data(**kwargs)
context['other_form'] = self.other_form
return context
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2705 次 |
| 最近记录: |