所以我做了一个简单的web应用程序,你输入一些文本,它就会显示在屏幕上,有点像聊天屏幕。
我的Python代码
import sys
from flask import *
app = Flask(__name__)
option_list = []
@app.route('/', methods=['POST', 'GET'])
def sessions():
if request.method == 'POST':
if request.form['message'] != '':
option_list.append(request.form['message'])
print(option_list, file = sys.stdout)
return render_template("session.html", option_list = option_list)
if __name__ == '__main__':
app.run(debug = True)
我的HTML代码
<html>
<body>
<title>Text:</title>
<ul style="list-style-type:none;"></ul>
{% for o in option_list %}
<li>{{ o }}</li>
{% endfor %}
</ul>
<form action = "http://127.0.0.1:5000/" method = "post">
<p><input type = "text" name = "message" autocomplete="off" /></p>
<p><input type = "submit" name = sgnin value = "Send" /></p>
</form>
</body>
</html>
日志和html都显示他们正在发送和接收数据。但是,它不打印O
的值,而是只打印O
。我很确定这是因为HTML文件。我肯定这可能是个新手犯的错误,但你知道怎么解决吗?
更改返回格式使用return render_template(“session.html”,{'option_list':option_list)
import sys
from flask import *
app = Flask(__name__)
option_list = []
@app.route('/', methods=['POST', 'GET'])
def sessions():
if request.method == 'POST':
if request.form['message'] != '':
option_list.append(request.form['message'])
print(option_list, file = sys.stdout)
return render_template("session.html", {'option_list': option_list)
if __name__ == '__main__':
app.run(debug = True)