【发布时间】:2018-09-12 20:07:55
【问题描述】:
我已经使用 Flask 在 Python 中实现了一项服务,以创建服务器。我的服务 (MyService) 接受用户的查询并返回响应,就像聊天机器人一样。因此,我想返回修改 Html 模板的文本响应和包含将服务用作命令行的响应的 json。 目前我的服务只返回一个渲染模板,我该怎么办?
我的应用:
app = Flask(__name__)
@app.route("/")
def main():
return render_template('index.html')
@app.route("/result", methods=['POST', 'GET'])
def result():
if request.method == 'POST':
query = request.form['query']
response = MyService.retrieve_response(query)
return render_template("index.html", value=response)
if __name__ == "__main__":
app.run()
还有我的简单 index.html:
<!DOCTYPE html>
<html lang="en">
<body>
<h2>Wellcome!</h2>
<form action="http://localhost:5000/result" method="POST">
Make a question:<br>
<br>
<input type="text" name="query" id="query">
<br><br>
<input type="submit" value="submit"/>
</form>
<br>
<h3>Response is: </h3>
<br>
{{value}}
</body>
</html>
【问题讨论】:
-
嗯,这看起来应该对我有用,你能尝试重新解释什么不起作用吗?
-
现在我的应用只返回一个render_template,我想返回一个json和一个render_template。
标签: python html json templates flask