Django表单向导渲染到步骤响应

时间:2018-01-09 03:28:29

标签: python django views django-formwizard render-to-response

我正在使用Django表单向导,我正在尝试模拟典型的

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

但没有重定向,仅在步骤中。

所以想法就是我有一个这样的表单向导

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')

然后在模板ID中有这样的东西

{% 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 %}

所以只是重申一下克莱尔的问题。我想:

  1. 从上一步获取数据(比如说第一步和名称)
  2. 在第二步中将该数据传递给相应的html 模板{{name}}
  3. 渲染最后一步,以便我可以看到名称 前端
  4. 提前感谢您的帮助

2 个答案:

答案 0 :(得分:1)

使用get_form方法,也许这样的事情可以解决问题:

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

这样,您将获得第一步表单中的first_name字段,作为第三步表单中第一个字段的标签

答案 1 :(得分:0)

您可以使用WizardView子类的get_context_data()方法提供其他上下文变量。

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