[英]How to send data from flask to html template
I have a small application where input is taken from the user and based on it data is displayed back on the html. 我有一个小应用程序,从用户那里获取输入,并根据它将数据显示在html上。 I have to send data from flask to display on html but unable to find a way to do it. 我必须从flask发送数据到html上显示但无法找到方法。 There's no error that I encountered. 我遇到没有错误。
[UPDATED]: Python script : [更新]:Python脚本:
from flask import Flask, render_template, request
import random
app = Flask(__name__, template_folder='templates')
@app.route('/', methods=['GET','POST'])
def samplefunction():
if request.method == 'GET':
return render_template('new.html')
if request.mthod == 'POST':
greetIn = ['hey', 'hi', 'hey there', 'hi there', 'hello', 'hola', 'yoo']
byeIn = ['bye', 'see you later', 'catch you later', 'toodles']
nameOut = ['my name is Fatty!!', 'Fatty is my name', 'you can call me Fatty', 'I go by the name of Fatty']
greetOut = ['hey there!', 'hi!', 'hi there!', 'hey!']
byeOut = ['bye bye', 'bye. see you later']
human1 = request.form['human']
if human1 in greetIn:
bot = random.choice(greetOut)
return render_template('new.html', bot=bot)
else:
bot = 'Sorry..no idea!!!'
return render_template('new.html', bot=bot)
if __name__ == "__main__":
app.run(debug=True)
HTML code : HTML代码:
<html>
<head>
<title>BOT</title>
<script>
var bot = {{ bot }}
</script>
</head>
<body>
<h1>Hello, type something to begin!</h1>
<form method='post'>
Human: <input type='text' name='human'><br>
Bot: {{ bot }}<br>
<input type="submit" name="action">
</form>
</body>
</html>
Any help would be appreciated. 任何帮助,将不胜感激。
Thank You! 谢谢!
Problem 问题
Your function samplefunction()
always renders new.html
without a variable included because of the initial return statement. 由于初始return语句,函数samplefunction()
总是呈现new.html
而不包含变量。
def samplefunction():
return render_template('new.html')
Fix 固定
You need to add if request.method == 'GET':
and if request.method == 'POST':
, and then your code will work. 你需要添加if request.method == 'GET':
if request.method == 'POST':
那么你的代码就可以了。 And change the variable name as the other answer by @Harrison suggests: bot = random.choice(greetOut)
not bot = random.choice(greetout)
并通过@Harrison建议更改变量名称作为另一个答案: bot = random.choice(greetOut)
not bot = random.choice(greetout)
Updated Code 更新的代码
def samplefunction():
if request.method == 'GET':
return render_template('new.html')
if request.method == 'POST':
greetIn = ['hey', 'hi', 'hey there', 'hi there', 'hello', 'hola', 'yoo']
byeIn = ['bye', 'see you later', 'catch you later', 'toodles']
nameOut = ['my name is Fatty!!', 'Fatty is my name', 'you can call me Fatty', 'I go by the name of Fatty']
greetOut = ['hey there!', 'hi!', 'hi there!', 'hey!']
byeOut = ['bye bye', 'bye. see you later']
human1 = request.form['human']
if human1 in greetIn:
bot = random.choice(greetOut)
return render_template('new.html', bot=bot)
else:
render_template('new.html')
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True)
greetOut = ['hey there!', 'hi!', 'hi there!', 'hey!']
<-- greet 0 ut greetOut = ['hey there!', 'hi!', 'hi there!', 'hey!']
< - 问候0 ut
bot = random.choice(greetout)
<-- greet o ut bot = random.choice(greetout)
< - greet o ut
Other than that, your templating looks fine. 除此之外,你的模板看起来很好。 If you correct your capitalization it should work. 如果你纠正你的大写,它应该工作。
Additionally, not mandatory but you might as well condense your Flask imports into 1 line like this: from flask import Flask, render_template, request
另外,不是强制性的,但您可以将Flask导入压缩成1行,如下所示: from flask import Flask, render_template, request
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.