本文介绍了通过django FormWizard传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个django表单向导,让人们注册一个
事件。我可以通过表单向导查看 done 方法中的数据。
问题是我还需要 event_id 传入完成。如何从url通过表单向导获取
event_id 完成?简单的例子?

  ------- 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 = {}
form_list中的表单:
data.update(form.cleaned_data)
打印数据
#我需要event_id就在这里。如何得到它?
返回render_to_response('done.html',{
'form_data':form_list中的表单的form.cleaned_data],
})
/ pre>

解决方案

我想你必须把它放在窗体中,才能从那里得到。



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

  def wrapper_view(request,id):
#somecode
seats_instance = SeatsModel。 object.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其他表单
...
#using seatinst get event id

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


I am trying to build a django form wizard to allow people to register for anevent. I can get through the form wizard and see data in the done method.The problem is that I also need event_id passed into done also. How do I getevent_id from the url through the form wizard and into done? Simple example?

------- 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],
    })
解决方案

I think you will have to put that in the form and get it from there.

If its model form you can pass instance_dict param to the wizard view. instance_dict param. However in that case you will have to implement a wrapper view that will prepare the wizard view with these params. Something like this:

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

这篇关于通过django FormWizard传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 00:49