2

我正在尝试构建一个 django 表单向导来允许人们注册一个活动。我可以通过表单向导查看done方法中的数据。问题是我也需要event_id传入 done 。如何 event_id通过表单向导从 url 获取并进入done?简单的例子?

------- urls.py ---------
named_register_forms2 = (
    ('basicdata', SeatsForm),
    ('form2', AnotherForm),
)

urlpatterns = patterns('',
    url(r'^register/(?P<event_id>\d+)/$', register_wizard, name='register_step'),
)

------ forms.py -----------
class SeatsForm(forms.ModelForm):

  class Meta:
    model = MyModel
    fields = [ 'last_name', 'first_name', 'address1', 'address2', 
               'city', 'state', 'zipcode', 'phone_number', 'email']

  def __init__(self, *args, **kwargs):
      super(SeatsForm, self).__init__(*args, **kwargs)

class RegisterWizard(SessionWizardView):
    #storage_name = 'formtools.wizard.storage.session.SessionStorage'
    template_name = 'wizard_form.html'

    def done(self, form_list, **kwargs):
        data = {}
        for form in form_list:
                data.update(form.cleaned_data)
                print data
        # I need event_id right here.  How to get it?
        return render_to_response('done.html', {
            'form_data': [form.cleaned_data for form in form_list],
    })
4

2 回答 2

4

我认为您必须将其放入表格并从那里获取。

如果它的模型形式,您可以将instance_dict参数传递给向导视图。instance_dict 参数。但是,在这种情况下,您将必须实现一个包装视图,该视图将使用这些参数准备向导视图。像这样的东西:

def wrapper_view(request, id):
    #somecode
    seats_instance = SeatsModel.objects.get(id=id)
    another_instance = AnotherModel.objects.get(id=id)
    inst_dict = { '0': seats_instance,
                  '1': another_instance
                }
    return RegisterWizard.as_view(named_register_forms2, instance_dict=inst_dict)(request)

class RegisterWizard(SessionWizardView):
    #storage_name = 'formtools.wizard.storage.session.SessionStorage'
    template_name = 'wizard_form.html'

    def done(self, form_list, **kwargs):
        data = {}
        seatform= form_list[0]
        seatinst = form.save()    
        #save other forms
        ...
        #using seatinst get event id

        return render_to_response('done.html', {
            'form_data': [form.cleaned_data for form in form_list],
             })
于 2012-08-07T08:54:57.033 回答
4

这个问题已经有两年了,但为了完整起见,我一直在通过覆盖dispatch向导类的方法来解决这个问题。向导使用来自 URL 调度程序的原始参数调用此方法。您可以在instance_dict其他任何事情发生之前修改向导(可能还有其他任何向导成员)。

class RegisterWizard(SessionWizardView):
    #storage_name = 'formtools.wizard.storage.session.SessionStorage'
    template_name = 'wizard_form.html'

    def dispatch(self, request, id, *args, **kwargs):
        self.instance_dict = {
            '0': SeatsModel.objects.get(id=id),
            '1': AnotherModel.objects.get(id=id),
        }
        return super(RegisterWizard, self).dispatch(request, *args, **kwargs)

    def done(self, form_list, **kwargs):
        data = {}
        seatform= form_list[0]
        seatinst = form.save()    
        #save other forms
        ...
        #using seatinst get event id

        return render_to_response('done.html', {
            'form_data': [form.cleaned_data for form in form_list],
        })

我不确定是否有任何主要的功能优势,但感觉就像向导更加封装了这种方式。此外,我不知道这是否是该dispatch方法的预期用途。

我想如果一个人要继承RegisterWizard设置instance_dict对象的行为,SeatsModel并且AnotherModel不需要让用户使用包装函数就可以使用;这可能是这样做的唯一实际优势。

于 2014-10-20T05:26:07.107 回答