Django FormWizard NamedUrl问题

时间:2015-08-12 20:43:00

标签: python django django-formwizard

我尝试将NamedUrlSessionWizardView与我的表单向导一起使用,以使表单的每一步都拥有它自己的独立URL。但是,似乎文档很少,所以我很难让它正常工作。

现在我可以输入网址(example.com/form/1)并显示每一步。但是由于某些原因,模板没有显示每个步骤的字段,因此我无法输入任何信息。我检查了django调试工具栏,它说表单无效:

'wizard': {'form': <CompanyDetailsForm bound=True, valid=False, fields=(name;state;city;funding_level;products_offered)>

Urls.py:

from .views import DataRequestWizardView, step_3_condition, step_4_condition, data_request_forms
from . import views

urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name="landing.html")),
    url(r'^start/$', TemplateView.as_view(template_name="start.html"), name='start'),
    url(r'^form/(?P<step>.+)/$', DataRequestWizardView.as_view(data_request_forms, url_name="product_wizard_step"), name='product_wizard_step'),
    url(r'^form/$', DataRequestWizardView.as_view(data_request_forms, url_name="product_wizard_step"), name='product_wizard'),
]

观点:

    data_request_forms = (
        ("1", forms.CompanyDetailsForm),
        ("2", forms.DataRequestForm1),
        ("3", forms.DataRequestForm2),
        ("4", forms.DataRequestForm3),
        ("5", forms.DataRequestForm4),
        ("6", forms.DataRequestForm5)
    )

class DataRequestWizardView(NamedUrlSessionWizardView):    
    instance = None
    def get_template_names(self):
        return ['step_{0}_template.html'.format(self.steps.current)]

    def get_step_url(self, step):
        return reverse('canareeform:product_wizard_step', kwargs={'step':step})

    def get_form_instance( self, step ):
        if self.instance is None:
            self.instance = DataRequest()
        return self.instance

    def done( self, form_list, form_dict, **kwargs ):
        form_dict={}
        for x in form_list:
            form_dict=dict(form_dict.items()+x.cleaned_data.items())
        company = CompanyDetail(name=form_dict['name'], state=form_dict['state'], city=form_dict['city'], funding_level=form_dict['funding_level'], products_offered=form_dict['products_offered'])
        company.save()
        request = self.instance
        request.company = company
        request.save()
        return render_to_response('done.html', {'form_data' : form_data})

在我更改为NamedUrl之前它正在工作,然后只进行了一些编辑,字段没有显示。

1 个答案:

答案 0 :(得分:0)

所以我明白了。我的模板文件名为:step_0_template.html,step_1_template等。此功能提供了正确的模板:

 def get_template_names(self):
    return ['step_{0}_template.html'.format(self.steps.current)]

然而,当我更改为NamedUrl时,它开始将模板从1开始索引而不是0.所以我只是在所有模板名称中添加了1。