首页 > 解决方案 > 使用重定向将烧瓶中的值从表单传递到表

问题描述

我正在开发这个应用程序,它有一个表单,您可以在其中输入名称,从下拉框中选择一个值,然后从复选框中选择一个或多个项目。我需要将此表单的值重定向到另一个页面并将它们格式化为表格。我是编程新手,想从添加表单中获取值并将它们格式化为计数表单中的表格。这是我的代码片段:

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

标签: flask

解决方案


使用redirect(url_for('/tally', key=key)) Wherekey是变量的字典,formation_name并且variation_name


推荐阅读