使用重定向将flask中的值从窗体传递到表
我正在开发这个应用程序,它有一个表单,您可以在其中输入名称,从下拉框中选择一个值,并从复选框中选择一个或多个项目。我需要将这个表单的值重定向到另一个页面,并将它们格式化到一个表中。我是编程新手,希望从add表单中获取值,并将它们格式化为tally表单中的表。以下是我的代码片段:使用重定向将flask中的值从窗体传递到表,flask,Flask,我正在开发这个应用程序,它有一个表单,您可以在其中输入名称,从下拉框中选择一个值,并从复选框中选择一个或多个项目。我需要将这个表单的值重定向到另一个页面,并将它们格式化到一个表中。我是编程新手,希望从add表单中获取值,并将它们格式化为tally表单中的表。以下是我的代码片段: @app.route('/add', methods=['POST','GET']) def add(): if request.method == 'POST': plan_name = req
@app.route('/add', methods=['POST','GET'])
def add():
if request.method == 'POST':
plan_name = request.form
formation_name = request.form
variation_name = request.form
return redirect('/tally')
return render_template('add.html')
@app.route('/tally', methods=['Get','POST'])
def tally():
return render_template('tally.html')
<form action='/add' method='POST'>
<h1>Enter a name for your gameplan:</h1>
<div class="form-group row">
<label for="plan" name = 'plan_name'value = '{{request.form['name']}}' class="col-sm-2 col-form-label">Name:</label>
<div class="col-sm-10">
<input type="text" >
</div>
</div>
<p><h1>Choose Your Formation<p></h1>
<div class="form-row align-items-center">
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect">
<option selected>Choose...</option>
<option name = "formation_name" >Doubles</option>
</select>
</div>
</div>
<p><h1>Select The Variation</h1></p>
<div class="form-group row">
<div class="col-sm-2">Select one or many:</div>
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" name = 'variation_name' type="checkbox" id="split">
<label class="form-check-label" for="split">
Split
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="nearPistol">
<label class="form-check-label" for="nearPistol">
Near Pistol
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="farPistol">
<label class="form-check-label" for="farPistol">
Far Pistol
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="queen">
<label class="form-check-label" for="queen">
Queen
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="right">
<label class="form-check-label" for="right">
Right
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="left">
<label class="form-check-label" for="left">
Left
</label>
</div>
</div>
</div>
<div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Add Another Formation</button>
</div>
</div>
</div>
</form>
{%endblock%}
{%for key, values in add %}
<thead >
<tr>
<th scope="col">{{key.formation_name}}</th>
<th scope="col">Left</th>
<th scope="col">Right</th>
</tr>
</thead>
<tbody>
<tr>
<td scope="row">{{values.variation_name}}</td>
<td>
<div class='container'>
<button type="button" class="btn btn-outline-primary">Run</button>
<button type="button" class="btn btn-outline-primary">Pass</button>
</div>
</td>
<td>
<div class='container'>
<button type="button" class="btn btn-outline-primary">Run</button>
<button type="button" class="btn btn-outline-primary">Pass</button>
</div>
</td>
</tr>
</tbody>
{% endfor %}
</table>
<div class="col-sm-10 container">
<button type="submit" class="btn btn-primary">Tally Results</button>
</div>
{%endblock%}
@app.route('/add',methods=['POST','GET'])
def add():
如果request.method==“POST”:
计划名称=request.form
formation\u name=request.form
变体名称=request.form
返回重定向(“/tally”)
返回渲染模板('add.html')
@app.route('/tally',methods=['Get','POST'])
def tally():
返回渲染模板('tally.html')
输入游戏计划的名称:
姓名:
选择你的阵型
选择。。。
双打
选择变体
选择一个或多个:
分裂
近手枪
远手枪
女王
赖特
左边
拯救
再加一个队形
{%endblock%}
{键为%,添加%中的值为}
{{key.formation_name}
左边
赖特
{{values.variation_name}
跑
通过
跑
通过
{%endfor%}
理货结果
{%endblock%}
使用重定向(url\u用于('/tally',key=key))
其中,key
是变量的字典formation\u name
和variation\u name