Django 表单向导在步骤中呈现响应
Django Form Wizard Render to Response on Step
我正在使用 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 %}
为了清楚起见,我只是重申一下这个问题。我想:
- 获取上一步的数据(比如第一步和名称)
- 在第二步中将该数据传递给相应的 html
模板{{name}}
- 渲染最后一步,这样我就可以看到名字了
前端
提前感谢您的帮助
也许像这样的方法可以解决问题,使用 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,作为第三步表单第一个字段的标签
您可以使用 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
我正在使用 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 %}
为了清楚起见,我只是重申一下这个问题。我想:
- 获取上一步的数据(比如第一步和名称)
- 在第二步中将该数据传递给相应的 html 模板{{name}}
- 渲染最后一步,这样我就可以看到名字了 前端
提前感谢您的帮助
也许像这样的方法可以解决问题,使用 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,作为第三步表单第一个字段的标签
您可以使用 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