[Django]-How to get django form wizard extra_context to show on template?

2👍

So what I ended up using on the template was:

{{ptype}}

which I had already tried.

The problem, and I’m still not sure why was that I had:

def process_step(self, request, form, step):
        if step == 1:
            self.extra_context = {'ptype': form.cleaned_data}
            return
        else:
            return

and what worked was:

def process_step(self, request, form, step):
        self.extra_context = {'ptype': 'hello!!',}

For some reason, the ‘step’ that is being passed to ‘process_step()’ is always == 0 which made my ‘if step ==1:’ logic fail…

After reviewing the source (django.contrib.formtools.wizard.FormWizard), one thing that looks like it could be failing on is my form is not valid. It must be valid for the step number to increment and call the process_step function. HOWEVER, the {{step}} variable is getting the right value. And I’m not doing anything crazy with the form…

So weird. But my main question is solved.

5👍

Looking at the docs i’d say that get_context_data is what you are after:

Returns the template context for a step. You can overwrite this method
to add more data for all or some steps. This method returns a
dictionary containing the rendered form step.

👤arie

Leave a comment