【问题标题】:Using different templates with django form wizard在 django 表单向导中使用不同的模板
【发布时间】:2012-05-16 10:55:28
【问题描述】:

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

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

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'

...........

文档说了一些关于 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],
                })

但出现错误:

get_template_names() takes exactly 2 arguments (1 given)

【问题讨论】:

    标签: django django-forms django-formwizard


    【解决方案1】:

    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)]
    

    我不确定current 是字符串还是整数还是什么 - 但看看视图,您应该会发现一个有用的“找不到名为 X 的模板”错误。

    【讨论】:

    猜你喜欢
    • 2016-01-17
    • 2016-03-05
    • 2014-02-06
    • 1970-01-01
    • 2013-01-16
    • 2019-04-29
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    相关资源
    最近更新 更多