【发布时间】:2017-12-26 21:08:15
【问题描述】:
我有一个小型应用程序,其中从用户那里获取输入,并根据它在 html 上显示数据。我必须从烧瓶中发送数据以显示在 html 上,但找不到方法。我没有遇到任何错误。
[更新]: 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 代码:
<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>
任何帮助将不胜感激。
谢谢!
【问题讨论】:
-
这对我来说是正确的。它用什么代替
{{ bot }}? -
bot = random.choice(greetout)是它在您的代码中的实际样子吗?你从来没有在out中用小写的o声明变量。虽然我希望这会引发错误...... -
如上所述,一旦您将变量更改为
greetOut,它应该可以工作 -
@CoryMadden Ohh..没有看到它..但它仍然无法正常工作。页面只是刷新而不写任何东西来代替 {{bot}}
标签: python html python-3.x web flask