【发布时间】:2021-11-02 17:21:17
【问题描述】:
我正在尝试使用 Flask 创建一个网页,该网页根据用户选择的单选按钮更改其内容。我不想重定向到一个全新的网页,我只是希望用户能够单击一个选项,然后将该选项显示给他们。我试图使下面的示例尽可能简单。这是来自python app main.py的代码
@app.route('/', methods = ['GET', 'POST'])
def index():
return render_template("index.html")
def index(choice):
return render_template('index.html', choice = choice)
def index():
if request.method == 'POST':
selection = request.form['options']
return render_template('index.html', selection)
else:
return render_template('index.html')
这是index.html上的代码:
<form action="#" method='POST'>
<p>Two buttons: </p>
<p><input type = 'radio' name = 'options' value='YES'/>YES</p>
<p><input type = 'radio' name = 'options' value='NO'/>NO</p>
<p><input type= 'submit' value='submit'/></p>
</form>
{% if choice == 'YES' %}
<h1>it works!</h1>
{% else %}
<h1>nothing received</h1>
{% endif %}
我遇到的问题是 else 语句总是被执行,所以我从来没有看到网页显示“它有效!”。我也试过让 if 语句说 {% if choice %} 但它永远不会评估为真。
任何了解 Flask 的人都可以看到我在这里做错了什么吗?
【问题讨论】:
标签: python html flask radio-button