Python Flask 提交按钮问题

Python Flask submit button problems

"send..." 按钮不起作用,索引页是 运行,下拉菜单正常,但是当我单击 "send..." 时没有任何反应。 (Result.html 可以,但当然是空的)任何想法,问题是什么?

我有这些 .py 文件:

from flask import Flask, request, render_template
from flask_wtf import Form
from flask_wtf import FlaskForm

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def index():
    return render_template('index.html')

@app.route('/result/', methods=['POST', 'GET'])
def result():
    vehicle = request.form.get('wine')
    year = request.form.get('year')
    return render_template('result.html', vehicle=vehicle, year=year)

当然还有两个。html。 index.html:

<!DOCTYPE html>
<html>

<body>
<h1>Submitting</h1>

<form action="/result" method="POST">
    <label for="wine">wine</label>
    <select id="wine" name="wine">
      <option>red</option>
      <option>white</option>
      <option>rose</option>
    </select>

    <label for="year">Year</label>
    <select id="year"name="year">
      <option>1972</option>
      <option>1999</option>
      <option>2010</option>
    </select>
</form>

<br>
<button type="submit" value="submit">send...</button>
</body>
</html>

result.html:

<!DOCTYPE html>
<html>
    <body>
        {{ vehicle }} {{ year }}
    </body>
</html>

也许您需要将 button 放在 form 标签中?

<form action="/result" method="POST">
    ...
    <button type="submit" value="submit">send...</button>
</form>

用这个 HTML 替换你的 HTML。您需要将提交按钮放在表单标签内才能使其正常工作。

<body>
<h1>Submitting</h1>

<form action="/result" method="POST">
    <label for="wine">wine</label>
    <select id="wine" name="wine">
      <option>red</option>
      <option>white</option>
      <option>rose</option>
    </select>

    <label for="year">Year</label>
    <select id="year"name="year">
      <option>1972</option>
      <option>1999</option>
      <option>2010</option>
    </select>
    <button type="submit" value="submit">send...</button>

</form>

<br>
</body>
</html>