Python 如何让django表单向导的额外上下文显示在模板上?
编辑:FWIW,我正在运行django 1.3 我有Python 如何让django表单向导的额外上下文显示在模板上?,python,django,forms,django-formwizard,Python,Django,Forms,Django Formwizard,编辑:FWIW,我正在运行django 1.3 我有 class CreateProductWizard(FormWizard): def get_template(self, step): if step == 1: return 'product/form_wizard/editor.html' else: return 'product/form_wizard/wizard_%s.html' % ste
class CreateProductWizard(FormWizard):
def get_template(self, step):
if step == 1:
return 'product/form_wizard/editor.html'
else:
return 'product/form_wizard/wizard_%s.html' % step
def process_step(self, request, form, step):
if step == 1:
self.extra_context = {'ptype': form.cleaned_data}
return
else:
return
def done(self, request, form_list):
# now that it's all together, store it.
return render_to_response('product/form_wizard/done.html',
{'form_data': [form.cleaned_data for form in form_list]},
context_instance=RequestContext(request))
我想将self.extra_上下文添加到模板中
我如何在模板上得到它
我已尝试使用该模板:
{{extra_context}}
{{form.extra_context}}
{{form.extra_context.ptype}}
等等看看我想说的是你想要的:
返回步骤的模板上下文。您可以覆盖此方法
为所有或某些步骤添加更多数据。此方法返回一个
包含呈现表单步骤的字典
因此,我最终在模板上使用的是:
{{ptype}}
我已经试过了
问题出在我身上,我仍然不知道为什么我会:
def process_step(self, request, form, step):
if step == 1:
self.extra_context = {'ptype': form.cleaned_data}
return
else:
return
有效的方法是:
def process_step(self, request, form, step):
self.extra_context = {'ptype': 'hello!!',}
由于某些原因,传递给“process_step()”的“step”总是==0,这使得我的“if step==1:”逻辑失败
在查看源代码(django.contrib.formtools.wizard.FormWizard)之后,有一件事情看起来可能会失败,那就是我的表单无效。步骤编号必须有效才能递增并调用process_step函数。但是,{step}}变量得到了正确的值。而且我对表格没有做任何疯狂的事情
真奇怪。但我的主要问题已经解决了 该死。看起来这是针对开发版本的,我正在运行1.3。不过,他们添加的所有内容看起来都很不错!差不多五年后,我(再次)在谷歌上搜索这个问题,并找到了关于堆栈溢出的问题。这次你的回答是我的答案。哈@teewuane我知道这已经非常古老了,但问题是这个步骤是一个字符串。至少现在有了1.9版Django的
formtools.wizard
。@thefoxrocks,哈,我想回忆一下我在这里做的工作。但是知道step
是一个字符串而不是一个整数会根据我的答案产生不同:)但是python不会试图强制step
为整数并比较这些数字吗?还是我只是在想javascript?哈哈。@teewuane我想你在想JS。我现在可以检查字符串了。这似乎也很奇怪,他们会发送一个字符串作为一个步数,而不是一个基本的整数,虽然。。。我相信这一点让成千上万的人感到困惑:D