Formwizard with custom request

I have a problem with formwizard. In step 3 of the wizard, I use a filtered query, which depends on the choice from steps 1 and 2. My wizard works fine when moving forward, but when the user clicks the previous one or changes the choice in steps 1 and 2, everything goes wrong (validation error or filters will not work).

Here are my forms.py:

class BoekForm3(forms.Form): Activiteit = forms.ModelChoiceField(queryset=Activiteit.objects.all(), empty_label="(Nothing)") 

If I use queryset Activiteit.objects.none () in forms.py, the correct fields in step 3 of the wizard are displayed on the right, but step3 always gives a validation error.

If I use queryset Activiteit.object.all () in forms.py, the correct (filtered) fields are displayed in step 3 when going forward, and the form is validated. However, when the user clicks the previous step 4, the filter disappears and all options are available.

Edit2: ok I found out that get_form is read every time (removed my previous edit about it). The problem with my code is that when the user returns from step 4 to step 3, the get_form step is used, this is step4. I tried to change:

"if step == 'step3'"

in:

"if step == 'step3' or step == 'step4':"

in my opinion, but this leads to a key error in Activiteit

I think the fields are updated by going to step 3, but there is no form. I have included my view.py below.

 class BoekingWizard(SessionWizardView): def get_template_names(self): return [TEMPLATES[self.steps.current]] def get_context_data(self, form, **kwargs): plaatjes = Activiteitsgroepen.objects.all() context = super(BoekingWizard, self).get_context_data(form=form, **kwargs) if self.steps.current == 'step1': context.update({'plaatjes': plaatjes}) return context def get_form(self, step=None, data=None, files=None): form = super(BoekingWizard, self).get_form(step, data, files) if step == 'step3': x = self.get_cleaned_data_for_step('step1') or {} filter1 = x['Type_activiteit'].Groep y = self.get_cleaned_data_for_step('step2') or {} if y['Aantal_personen'] < 76: filter2 = 'kleinere groepen' elif y['Aantal_personen'] > 76: filter2 = 'grotere groepen' else: filter2 = 'geen selectie' reeks = ['1','2','3'] if y['Dagdeel'] in reeks: filter3 = 'Dagdeel' elif y['Dagdeel'] == '4': filter3 = 'Hele dag' elif y['Dagdeel'] == '5': filter3 = 'Middag + Avond' elif y['Dagdeel'] == '6': filter3 = 'Dag + Avond' form.fields['Activiteit'].queryset = Activiteit.objects.filter(Soort__Groep = filter1).filter(Grootte__Naam = filter2).filter(Dag__Omschrijving = filter3) return form 
+4
source share
2 answers

More and more, Formwizard does not support what I want. So I started working with normal forms and storing cleared_data in the session. The advantage is that I have more control, and I know better what is going on. Here is an idea:

 def BoekingForm(request): if request.method == 'POST': if request.POST.get('BoekForm2'): form1 = BoekForm1(request.POST) if form1.is_valid(): request.session['Data_BoekForm1'] = form1.cleaned_data if 'Data_BoekForm2' in request.session: x = request.session['Data_BoekForm2'] else: x = None form2 = BoekForm2(initial = x) return render_to_response('boeking/form2', { 'form2': form2, }, context_instance = RequestContext(request)) else: return render_to_response('boeking/form1', { 'form1': form1, }, context_instance = RequestContext(request)) if request.POST.get('BoekForm3'): form2 = BoekForm2(request.POST) 

If someone comes up with a solution, solve my question in Formwizard, I will have to mark my own answer as a solution.

0
source

To use custom step names, you must initialize the wizard class with a list of tuples of the step name and form class

 FORMS = [("step1", myapp.forms.Step1Form), ("step2", myapp.forms.Step2Form)] 

and in urls.py:

 urlpatterns = patterns('', (r'^wizard/$', BoekingWizard.as_view(FORMS)), ) 
+3
source

All Articles