flask - 使用重定向将烧瓶中的值从表单传递到表
问题描述
我正在开发这个应用程序,它有一个表单,您可以在其中输入名称,从下拉框中选择一个值,然后从复选框中选择一个或多个项目。我需要将此表单的值重定向到另一个页面并将它们格式化为表格。我是编程新手,想从添加表单中获取值并将它们格式化为计数表单中的表格。这是我的代码片段:
@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%}
解决方案
使用redirect(url_for('/tally', key=key))
Wherekey
是变量的字典,formation_name
并且variation_name
推荐阅读
- python-3.x - 将一个字段与包含逗号分隔的 4 个值的 csv 文件的另一个字段进行比较
- android - 找不到类“com.google.android.material.button.MaterialButton”
- python - 根据元组列表中的第二个元素查找具有前 5 个唯一值的所有项目
- nginx - 如何使用 Nginx 访问 control.unit.sock 以安全地代理 Unit API
- r - 如何从函数外部将 lm 对象中的权重作为变量传递,并将列名作为模型表单中的权重引用?
- python - 在 Python 中找到一个元素的总和
- database - 如何获取 Firebase 集合中的所有文档(包括不存在的祖先文档)?
- postgresql - Postgresql 函数:如何处理 [ IN() 或 ANY() ] 中的空值
- javascript - 将图像尺寸分配给文本位置
- mysql - 使用 sql_mode 在 mysql 中禁用 only_full_group_by