Django form wizard - choices depending on first form step Django form wizard - choices depending on first form step django django

Django form wizard - choices depending on first form step


I figured you could just access the POST dictionary directly in your __init__ method because it looks like the wizard passes POST into each form instance via get_form, but I don't see the data for some reason.

Instead of dwelling on that too long the alternative I've come up with is using the render_template hook.

class ContactWizard(FormWizard):    def done(selef, request, form_list):        return http.HttpResponse([form.cleaned_data for form in form_list])    def render_template(self, request, form, previous_fields, step, context=None):        """        The class itself is using hidden fields to pass its state, so        manually grab the location from the hidden fields (step-fieldname)        """        if step == 2:             locations = Location.objects.filter(location=request.POST.get('1-location'))            form.fields['locations'].choices = [(x, x) for x in locations]        return super(ContactWizard, self).render_template(request, form, previous_fields, step, context)


You can get the data from any step by using the storage object:

self.storage.get_step_data('0')

this will return the data dict saved in the storage backend for that particular step.

In my example below, the first form contains an 'Activity' drop down select. Then the second form contains a location select widget which only shows locations that are available for that activity.

This works when you go forward or backward through the wizard - the above answers don't work if you press 'prev' as they rely on the wizard going forward only (i.e. the POST dict won't contain step 0's data if you press prev on step 3!)

def get_form(self, step=None, data=None, files=None):    form = super(EnquiryWizard, self).get_form(step, data, files)    #print self['forms']['0'].cleaned_data    step = step or self.steps.current    if step == '1':        step_0_data = self.storage.get_step_data('0')        activity = Activity.objects.get(pk=step_0_data.get('0-activity'))        locations = Location.objects.filter(activities=activity)        form.fields['locations'].widget = forms.CheckboxSelectMultiple(choices=[(x.pk,x.name) for x in locations])    return form


The working code, after solving the problem with Yuji's help (Thank you) is:

class reMapWizard(FormWizard):    def render_template(self, request, form, previous_fields, step, context=None):        if step == 1:            location = request.POST.get('0-location')            address, lat, lng, country = getLocation(location)            form.fields['locations'] = forms.ChoiceField(widget=RadioSelect(), choices = [])            form.fields['locations'].choices = [(x, x) for x in address]        return super(reMapWizard, self).render_template(request, form, previous_fields, step, context)