开发者

How can I seed information for a form based on a previous form?

开发者 https://www.devze.com 2023-04-03 20:49 出处:网络
User submits form one. Form one is used as seed info for form 2 form_factory Using django form wizard after 3 hours of attempting to coax the开发者_开发技巧 stock Django 1.3 to use a form factory.

User submits form one.

Form one is used as seed info for form 2 form_factory

Using django form wizard after 3 hours of attempting to coax the开发者_开发技巧 stock Django 1.3 to use a form factory.

I'm trying to figure out how to seed this information. I have the information - I just don't know where to stick it. (Oh I have ideas..)

--urls.py--

url(r'homes/bulk/$', 
   BulkHomeWizard.as_view([('home_0', BulkUploadFormOne), 
                           ('home_1', formset_factory(BulkUploadFormTwo, extra=1))])

--views.py--

class BulkHomeWizard(SessionWizardView):

def get_context_data(self, form, **kwargs):
    context = super(BulkHomeWizard, self).get_context_data(form, **kwargs)
    self.template_name = 'axis/bulk_%s.html' %  self.steps.current
    if self.steps.current == 'home_1':
        data = self.get_cleaned_data_for_step('home_0')
        # OK I have the data.. Now I thought I could simply pass the form back in....
        HomeFormSet = formset_factory(BulkUploadFormTwo, extra=0)
        form = HomeFormSet(initial=data['homes'])

        context.update({'form': form})
    return context

If anyone knows these new form wizards would you mind giving me a once over. I'm sure it's simple...


There is a backport of Django 1.4 wizards for older Django version:

https://github.com/stephrdev/django-formwizard

You should use this instead of Django 1.3 wizard which is deprecated in 1.4. Your port to Django 1.4 will be easier.

You can prepare your port to Django 1.4 like this if you want to:

try:
    # Django 1.4
    from django.contrib.formtools.wizard.views import SessionWizardView
except ImportError:
    # For older django version use formwizard backport
    from formwizard.views import SessionWizardView


The key is SessionWizardView... This is in the development branch of Django and won't be released until 1.4. You can of course download the development branch and use the SessionWizardView but this isn't recommended for production code!

The older version of the forms wizard for 1.3 is documented here. It does much less (hence the new version) and basically passes everything around as hidden fields.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号