Django wizardview:如何在不验证表单的情况下设置步骤数据
Django wizardview: how to set up step data without validating the form
我想要做什么我有一个视图,它有一个快捷方式表单,它会要求用户在将用户重定向到向导表单之前预先填写一些字段。
例如:
class PreFillView():
def post(self,request):
# get the data from the form and save into request.session
# Then http redirect to the wizard view
然后从这个视图,我将它重定向到 WizardView。在向导视图中,我在调度函数中捕获了从前一个视图传入的所有信息:
class MyWizardView(NamedUrlSessionWizardView):
def dispatch(self,request, *args, **kwargs):
#parse data from request.session
#set step data using these data
# Note these data fields only partially covered the form in the wizardview, there is still a couple of fields needed to be filled in the wizard view.
这几乎可以正常工作,但唯一的问题是它会验证表单并针对未预填充的字段弹出字段错误。我试过了,如果我只重定向到向导视图而不设置步骤数据,就可以了。它不会验证表单,因此不会显示任何字段错误。
我是 Django 的新手,不确定我是否做对了,如果是,我如何避免在为当前步骤设置步骤数据后验证表单?任何帮助将不胜感激。
在您的向导视图中实施 WizardView.get_form_initial(step)
方法 class。
此方法获取 step
数字作为参数,它应该 return dict 作为该步骤的表单的初始数据。
我想要做什么我有一个视图,它有一个快捷方式表单,它会要求用户在将用户重定向到向导表单之前预先填写一些字段。
例如:
class PreFillView():
def post(self,request):
# get the data from the form and save into request.session
# Then http redirect to the wizard view
然后从这个视图,我将它重定向到 WizardView。在向导视图中,我在调度函数中捕获了从前一个视图传入的所有信息:
class MyWizardView(NamedUrlSessionWizardView):
def dispatch(self,request, *args, **kwargs):
#parse data from request.session
#set step data using these data
# Note these data fields only partially covered the form in the wizardview, there is still a couple of fields needed to be filled in the wizard view.
这几乎可以正常工作,但唯一的问题是它会验证表单并针对未预填充的字段弹出字段错误。我试过了,如果我只重定向到向导视图而不设置步骤数据,就可以了。它不会验证表单,因此不会显示任何字段错误。
我是 Django 的新手,不确定我是否做对了,如果是,我如何避免在为当前步骤设置步骤数据后验证表单?任何帮助将不胜感激。
在您的向导视图中实施 WizardView.get_form_initial(step)
方法 class。
此方法获取 step
数字作为参数,它应该 return dict 作为该步骤的表单的初始数据。