英文:
In Flask, how can I write a condition in a file py (not html) and print only result in html?
问题
I'm new to Flask. In the index.html
page, there are a combobox
and a textbox
. If I select the Red
item of the combobox, I would like the textbox to print "You have selected the item Red".
I would like to achieve this inside in the python file and not in the html file. I saw that conditions can also be written in the html file, but I don't want to write them there. So I would like to write the condition in the app.py
python file and NOT in the index.html
. Something like this: if colours.get() == "Red"
. Obviously, however, important, I would like to display the output in the textbox index.html
.
How can I get this? Thank you all!
Flask Code: app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route("/combobox", methods=["GET","POST"])
def dropdown():
colours = ["Red", "Blue", "Black", "Orange"]
return render_template("index.html", colours=colours)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8080)
Textbox and combobox code: index.html
<form name="Item_1" action="/getLigand" method="POST">
<select name="colours">
{% for colour in colours %}
<option value="{{ colour }}" SELECTED>{{ colour }}</option>
{% endfor %}
</select>
</form>
<form action="/result" method="POST">
<input type="text" name="result" />
</form>
Please note that I've translated the provided text and code for you.
英文:
I'm new to Flask. In the index.html
page, there are a combobox
and a textbox
. If I select the Red
item of the combobox, I would like the textbox to print "You have selected the item Red
".
I would like to achieve this inside in the python file and not in the html file. I saw that conditions can also be written in the html file, but I don't want to write them there. So I would like to write the condition in the app.py
python file and NOT in the index.html
. Something like this: if colours.get() == "Red"
. Obviously, however, important, i would like to display the output in the textbox index.html
How can i get this? Thank you all!
Flask Code: app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route("/combobox", methods=["GET","POST"])
def dropdown():
colours = ["Red", "Blue", "Black", "Orange"]
return render_template("index.html", colours=colours)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8080)
Textbox and combobox code: index.html
<form name="Item_1" action="/getLigand" method="POST">
<select name="colours">
{% for colour in colours %}
<option value="{{ colour }}" SELECTED>{{ colour }}</option>
{% endfor %}
</select>
</form>
<form action = "/result" method = "POST">
<input type="text" name="result" />
</form>
答案1
得分: 0
以下是已翻译好的内容:
这是一个可行的示例。这是app.py
:
from flask import Flask, render_template, request
app = Flask(__name__)
colours = ["红色", "蓝色", "黑色", "橙色"]
@app.route('/')
def index():
return render_template("index.html", colours=colours, chosen="黑色")
@app.route("/combobox", methods=["GET","POST"])
def dropdown():
picked = request.form['colours']
if picked == '红色':
message = "<<< 你选择了红色"
elif picked == '蓝色':
message = "<<< 你选择了蓝色"
elif picked == '黑色':
message = "<<< 你选择了黑色"
else:
message = "<<< 哦,不好,你选择了橙色"
return render_template("index.html", colours=colours, chosen=picked, message=message)
if __name__ == '__main__':
app.run(host="127.0.0.1", port=8080)
这是templates/index.html
:
<form name="Item_1" action="/combobox" method="POST">
<select name="colours">
{% for colour in colours %}
{% if colour == chosen %}
<option value="{{ colour }}" SELECTED>{{ colour }}</option>
{% else %}
<option value="{{ colour }}">{{ colour }}</option>
{% endif %}
{% endfor %}
</select>
<input type="submit">
</form>
<form action="/result" method="POST">
<input type="text" name="result" value="{{ message }}" />
</form>
跟进:
现在,写了这么多,我开始思考你是否希望在他们选择颜色后立即显示该消息,而无需单击“提交”按钮。如果这是你想要的,那可以做到,但这是一种非常不同的技术。所有这些都必须在Javascript中完成,作为嵌入到HTML中的代码。这完全在浏览器内处理。它不会发送任何内容到Web服务器,因此你的Python代码不会参与其中。
英文:
Here is an example that works. This is app.py
:
from flask import Flask, render_template, request
app = Flask(__name__)
colours = ["Red", "Blue", "Black", "Orange"]
@app.route('/')
def index():
return render_template("index.html", colours=colours, chosen="Black")
@app.route("/combobox", methods=["GET","POST"])
def dropdown():
picked = request.form['colours']
if picked == 'Red':
message = "<<< You chose red"
elif picked == 'Blue':
message = "<<< You chose blue"
elif picked == 'Black':
message = "<<< You chose black"
else:
message = "<<< Oh no, you chose orange"
return render_template("index.html", colours=colours, chosen=picked, message=message)
if __name__ == '__main__':
app.run(host="127.0.0.1", port=8080)
This is templates/index.html
:
<form name="Item_1" action="/combobox" method="POST">
<select name="colours">
{% for colour in colours %}
{% if colour == chosen %}
<option value="{{ colour }}" SELECTED>{{ colour }}</option>
{% else %}
<option value="{{ colour }}">{{ colour }}</option>
{% endif %}
{% endfor %}
</select>
<input type="submit">
</form>
<form action = "/result" method = "POST">
<input type="text" name="result" value="{{ message }}" />
</form>
Followup
Now, having written all that, I'm starting to wonder if you wanted that message to show up as soon as they PICK the color, without having to click a "Submit" button. If that's what you're after, that can be done, but it's a very different technique. That all has to be done in Javascript, as code that you embed within your HTML. That's all handled entirely within the browser. It doesn't send anything to the web server, so your Python code will not be involved.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论