0

我一直在在线学习教程,但是在尝试有条件地在表单向导中显示步骤时遇到了困难。

视图.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],
                })

网址.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
        })),
.......

表格.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)

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

我认为我的silver_ad_selectedplatinum_ad_selected方法可能是问题所在,但我不确定。

谢谢

4

1 回答 1

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()
        })),
于 2012-05-07T22:56:02.327 回答