Django Form Wizard Render to Response on Step

1.7k views Asked by At

I am working with the Django form wizard and i am trying to emulate the typical

render_to_response('index.html',{'name', name})

but without redirect and only in the steps.

So the idea would be i have a form wizard like this

TEMPLATES = {"0": "MyApp/checkout_template1.html",
             "1": "MyApp/checkout_template2.html",
             "2": "MyApp/checkout_template3.html",
            }

class MyWizard(SessionWizardView):

    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

    def get_context_data(self, form, **kwargs):
        context = super(MyWizard, self).get_context_data(form=form, **kwargs)

        if self.steps.current == '3':
            data_1 = self.get_cleaned_data_for_step('1')

            ((HERE I NEED TO PASS THE data_1["first_name"] TO SHOW ON NEXT TEMPLATE))
            print(data_1["first_name"])
        return context

    def done(self, form_list, **kwargs):
        if self.request.method == 'POST':
            process_form_data(form_list)
        return render(self.request,'MyApp/congratulations.html')

then in templates id have something like this

{% extends "MyApp/base.html" %}
{% load staticfiles %}
{% load humanize %}
{% block head %}{{ wizard.form.media }}{% endblock %}
{% endblock %}

{% block body_block%}

  <div class="confirmation-body">
      <div class="confirmation-body-container">

            <div class="form-title-container">
              <h1>Information</h1>
            <p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
            <form action="/checkout/" method="post">{% csrf_token %}
                <table>
                {{ wizard.management_form }}
                {% if wizard.form.forms %}
                    {{ wizard.form.management_form }}
                    {% for form in wizard.form.forms %}
                    {{form}}
                    {% endfor %}
                {% else %}
                    <div class="checkout-title">Information</div>
                    //////////////////////
                    {{ name }} #something like this that prints in the next step
                    //////////////////////
                    {{wizard.form.name_on_card}}
                {% endif %}
                </table>
                <hr />
                <div class="checkout-nav-controls">
                  <div>
                    {% if wizard.steps.prev %}
                    <button id="prev-step-button" name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">back</button>
                    {% endif %}
                  </div>
                  <input id="my-submit-button" type="submit" value="Next"/>
                </div>

                </form>

      </div>
  </div>

{% endblock %}

So just to reiterate the question for clairity. I want to:

  1. get the data from the previous step (lets say first step and name)
  2. in the second step pass that data to the corresponding html template {{name}}
  3. render that last step so i can see the name on the front end

thanks in advance for your help

2

There are 2 answers

0
pelekoudasq On BEST ANSWER

Maybe something like this whould do the trick, using get_form method:

def get_form(self, step=None, data=None, files=None):
    form = super(MyWizard, self).get_form(step, data, files)
    if step is None:
        step = self.steps.current
    if step == '3':
        data_1 = self.get_cleaned_data_for_step('1')['first_name']
        form.fields['first_form_field'].label = data1
    return form

So in this way, you will have the field first_name from the first steps' form, as a label to the third step's form's first field

0
Deniz B. On

You can supply additional context variables by using the get_context_data() method of your WizardView subclass.

def get_context_data(self, form, **kwargs):
   context = super(MyWizard, self).get_context_data(form=form, **kwargs)
   if self.steps.current == 'my_step_name':
      category=self.get_cleaned_data_for_step('category')['name']
      context.update({'another_var': 'category'})
   return context

get_context_data() docs