Python 使用Flask在另一页中显示所选表单项

Python 使用Flask在另一页中显示所选表单项,python,html,twitter-bootstrap-3,flask,Python,Html,Twitter Bootstrap 3,Flask,我正在使用Flask0.12和Python3.6创建一个简单的应用程序,当单击submit按钮时,它将在另一个页面中显示选定的项目 主烧瓶应用程序位于app.py中,如下所示: from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/result', meth

我正在使用Flask0.12和Python3.6创建一个简单的应用程序,当单击submit按钮时,它将在另一个页面中显示选定的项目

主烧瓶应用程序位于
app.py
中,如下所示:

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/result', methods=['POST'])
def result():
    return render_template('result.html')
这将使用引导呈现以下网页:

<h1>Example Page</h1>

<p>Choose the options in the form below then submit your selections.</p>

<form action="">
  <div class="form-group">
    <label for="vehicle">Vehicle</label>
    <select id="vehicle" class="form-control">
      <option>truck</option>
      <option>car</option>
      <option>van</option>
    </select>
  </div>

  <div class="form-group">
    <label for="year">Year</label>
    <select id="year" class="form-control">
      <option>1972</option>
      <option>1999</option>
      <option>2010</option>
    </select>
  </div>
</form>

<br>

<button type="submit" class="btn btn-default">Submit</button>
示例页面
选择下表中的选项,然后提交您的选择

车辆 卡车 汽车 厢式货车 年 1972 1999 2010
提交

单击“提交”按钮时,如何让Flask在我的
results.html
模板中显示所选项目?

您必须对表单进行一些更改才能在结果页面中显示

  • 您必须在表单中添加操作url和方法

    <form action="/result" method="post">
    
  • 最后在结果html页面中添加以下内容

    {{ vehicle }} {{ year }}
    

  • 在步骤4中,如果是
    {{year}}
    而不是
    {{result}}
    ?修复了您答案中的步骤4,这似乎有效,谢谢
    from flask import request
    
    # inside your POST view
    vehicle = request.form.get('vehicle')
    year = request.form.get('year')
    return render_template('result.html', vehicle=vehicle, year=year)
    
    {{ vehicle }} {{ year }}