使用django表单向导使用不同的模板

Ale*_*and 3 django django-forms django-formwizard

我正在查看文档,我不太确定如何为每个步骤使用不同的模板...

我查看了源代码,似乎模板名称是硬编码的:

class WizardView(TemplateView):
    """
    The WizardView is used to create multi-page forms and handles all the
    storage and validation stuff. The wizard is based on Django's generic
    class based views.
    """
    storage_name = None
    form_list = None
    initial_dict = None
    instance_dict = None
    condition_dict = None
    template_name = 'formtools/wizard/wizard_form.html'

...........
Run Code Online (Sandbox Code Playgroud)

文档说了一些关于mixins的内容,但我不知道如何使用它们,因为我刚开始使用django ...

谢谢


更新:

我进一步研究了源代码并意识到有一种方法get_template_names.

我试过了:

class AddWizard(SessionWizardView):
        def get_template_names(self, step):
                if step == 0:
                        return 'business/add1.html'
                return 'business/add2.html'
        def done(self, form_list, **kwargs):
                return render_to_response('business/done.html', {
                        'form_data': [form.cleaned_data for form in form_list],
                })
Run Code Online (Sandbox Code Playgroud)

但是得到了一个错误:

get_template_names() takes exactly 2 arguments (1 given)

Yuj*_*ita 9

get_template_names不接受论点.你不能只为函数定义一个新的参数来接受并希望框架将它传递给它!(为了将来的故障排除)

WizardView源来看,看起来您可以访问当前活动的步骤self.steps.current,您可以在get_template_names视图中使用该步骤返回包含步骤的路径.

class AddWizard(SessionWizardView):
        def get_template_names(self):
            return ['step_{0}_template.html'.format(self.steps.current)]
Run Code Online (Sandbox Code Playgroud)

我不确定current是字符串还是整数还是什么 - 但是看一下这个视图你应该找到一个有用的"找不到名为X的模板"错误.