使用表单向导创建不同的完成路径 - Django

时间:2012-05-07 21:26:53

标签: django django-forms django-formwizard

我在网上关注了这个教程,但在尝试有条不紊地在表单向导中显示步骤时,我感到困惑。

views.py

def silver_ad_selected(wizard):
        cleaned_data = wizard.get_cleaned_data_for_step('0') or {}
        return cleaned_data.get('ad_type') == '2'

def platinum_ad_selected(wizard):
        cleaned_data = wizard.get_cleaned_data_for_step('0') or {}
        return cleaned_data.get('ad_type') == '3'

class AddWizard(SessionWizardView):
        def done(self, form_list, **kwargs):
                return render_to_response('business/done.html', {
                        'form_data': [form.cleaned_data for form in form_list],
                })

urls.py:

add_forms = [AddForm1, AddForm2, AddForm3]

urlpatterns = patterns('listings.views',
    url(r'^addWizard/$', AddWizard.as_view(add_forms,
        condition_dict = {
                '2': silver_ad_selected or premium_ad_selected
        })),
.......

forms.py

class AddForm1(forms.Form):
        TYPE_CHOICES = (
                ('1','Basic'),
                ('2','Silver'),
                ('3','Platinum')
        )
        ad_type = forms.ChoiceField(choices=TYPE_CHOICES, widget=forms.RadioSelect)

class AddForm2(forms.Form):
        category = forms.ModelChoiceField(queryset = Category.objects.all())
        city = forms.ModelChoiceField(queryset = City.objects.all())
        name = forms.CharField(max_length = 200)
        address = forms.CharField(max_length = 200)
        slogan = forms.CharField(max_length=140)
        phone = forms.CharField(max_length=10)
        website = forms.URLField()
        email = forms.EmailField()

class AddForm3(AddForm2):
        twitter = forms.CharField(max_length=100)
        facebook = forms.URLField()
        description = forms.CharField(widget=forms.Textarea)

基本上,我只想显示最后一步,如果用户选择“Silver”选项或“Platinum”选项,这是在步骤1中选择的。现在,无论我选择什么,只有前两个显示步骤/表格。

我认为我的silver_ad_selectedplatinum_ad_selected方法可能会出问题,但我不确定。

由于

1 个答案:

答案 0 :(得分:1)

尝试更改urls.py:

add_forms = [AddForm1, AddForm2, AddForm3]

urlpatterns = patterns('listings.views',
    url(r'^addWizard/$', AddWizard.as_view(add_forms,
        condition_dict = {
            '2': lambda wizard: wizard.silver_ad_selected() or wizard.premium_ad_selected()
        })),