我有两页长的表格。虽然,第一页提出了一个基本问题,并根据答案需要重定向到三个表格之一,然后提交。我创建了一个图表来帮助说明:
我正在使用django表单向导,但我不知道如何构建我的视图以便合并这种类型的逻辑。
有人可以帮助我开始这个吗? 最好显示一些需要在django-form SessionWizardView中执行的代码。
提前致谢。
答案 0 :(得分:3)
如果您完全依赖上一个表单中的数据,则不需要覆盖get_form,您可以使用condition_dict,在初始化WizardView时通过自定义ID映射表单。
我不喜欢在我的urls.py中定义WizardView,因此我将WizardView包装在标准视图函数中,并将普通视图函数放在urls.py中。
def buy_cart_wizard_view_wrapper(request):
con_dict = {FORM_ID_BILLING_ADDRESS_PICK: db_funcs.check_wizard_has_addresses_buy_cart,
FORM_ID_BILLING_ADDRESS: db_funcs.check_wizard_create_billing_pick_buy_cart,
FORM_ID_SHIPPING_ADDRESS_PICK: db_funcs.check_wizard_has_addresses_shipping_pick_buy_cart,
FORM_ID_SHIPPING_ADDRESS: db_funcs.check_wizard_create_shipping_pick_buy_cart,
FORM_ID_SAVED_BUSINESS_PAYMENT_PICK: db_funcs.check_wizard_has_payments_buy_cart,
FORM_ID_SAVED_BUSINESS_PAYMENT: db_funcs.check_wizard_create_payment_pick_buy_cart,
}
form_list = [(FORM_ID_BILLING_ADDRESS_PICK, accounts_forms.AddressPickerForm),
(FORM_ID_BILLING_ADDRESS, accounts_forms.BillingAddressForm),
(FORM_ID_SHIPPING_ADDRESS_PICK, accounts_forms.AddressPickerForm),
(FORM_ID_SHIPPING_ADDRESS, accounts_forms.AddressForm),
(FORM_ID_SAVED_BUSINESS_PAYMENT_PICK, accounts_forms.SavedBusinessPmtPickerForm),
(FORM_ID_SAVED_BUSINESS_PAYMENT, accounts_forms.SavedBusinessPmtBuyCartForm),
]
return BuyCartWizardView.as_view(form_list,condition_dict=con_dict, initial_dict=ini_dict)(request)
然后是上一个表格的条件检查示例'
def check_wizard_create_payment_pick_buy_cart(wizard):
"""
Check if the previous form has an address selected
"""
cleaned_data = wizard.get_cleaned_data_for_step(FORM_ID_SAVED_BUSINESS_PAYMENT_PICK) or {}
create_new_payment_ind = cleaned_data.get(FORM_FIELD_NAME) or ''
if create_new_payment_ind != '':
return True
else:
return False
返回True以将表单显示为下一步,返回False以跳过表单并在此之后移动到表单或完成(如果没有表单)。