这是我完成处理的方式
首先, urls.py :
url(r'^service/create/$','django_th.views.get_form_list', name='create_service'),
然后在 views.py中 :
我做了:
def get_form_list(request, form_list=None): if form_list is None: form_list = [ProviderForm, DummyForm, ConsummerForm, DummyForm, ServicesDescriptionForm] return UserServiceWizard.as_view(form_list=form_list)(request)
这允许使用以下命令定义5个步骤:
- 3点的已知,表格(
ProviderForm
,ConsummerForm
,ServicesDescriptionForm
- 2个未知的(
DummyForm
实际上是两次),将在下面动态处理
提供以下内容的 forms.py
DummyForm:
class DummyForm(forms.Form): pass
下一步是从ProviderForm中获取数据,从中获取我选择的服务,并加载此所选服务的:
在我的 views.py中 :
class UserServiceWizard(SessionWizardView): def __init__(self, **kwargs): self.form_list = kwargs.pop('form_list') return super(UserServiceWizard, self).__init__(**kwargs) def get_form_instance(self, step): if self.instance is None: self.instance = UserService() return self.instance def get_context_data(self, form, **kwargs): data = self.get_cleaned_data_for_step(self.get_prev_step( self.steps.current)) if self.steps.current == '1': service_name = str(data['provider']).split('Service')[1] #services are named th_<service> #call of the dedicated <service>ProviderForm form = class_for_name('th_' + service_name.lower() + '.forms', service_name + 'ProviderForm') elif self.steps.current == '3': service_name = str(data['consummer']).split('Service')[1] #services are named th_<service> #call of the dedicated <service>ConsummerForm form = class_for_name('th_' + service_name.lower() + '.forms', service_name + 'ConsummerForm') context = super(UserServiceWizard, self).get_context_data(form=form, **kwargs) return context
这里 :
__init__
从get_form_list
我定义的函数中加载数据urls.py
- 在
get_context_data
我需要改变DummyForm
在从我选择的下拉菜单中的服务步骤1和3ProviderForm
和ConsummerForm
。由于服务名为“ FoobarService”,因此我将“ Service”拆分Foobar(Consummer|Provider)Form
为class_for_name()
以下所示的服务形式调用:
class_for_name:
def class_for_name(module_name, class_name): m = importlib.import_module(module_name) c = getattr(m, class_name) return c
最后 :
有了这些,我可以随时随地动态更改表单,实际上,我决定在步骤1和3中进行更改,但可以在任何步骤中进行调整;)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)