Python 基本瓶形处理
我试图在Flask中创建一个基本表单,它将接收输入,对其进行操作,然后返回输出。我遇到了这样一个问题:当我运行终端并试图让我的应用程序在服务器上运行时,文件不可见。不知道bug在哪里 这是我整理文件的方式: /Users/eas/Desktop/grota/templates/index.html /Users/eas/Desktop/grota/templates/age.html /用户/eas/Desktop/grota/app.py 这是app.py文件Python 基本瓶形处理,python,flask,Python,Flask,我试图在Flask中创建一个基本表单,它将接收输入,对其进行操作,然后返回输出。我遇到了这样一个问题:当我运行终端并试图让我的应用程序在服务器上运行时,文件不可见。不知道bug在哪里 这是我整理文件的方式: /Users/eas/Desktop/grota/templates/index.html /Users/eas/Desktop/grota/templates/age.html /用户/eas/Desktop/grota/app.py 这是app.py文件 from flask impor
from flask import Flask, render_template,request
app = Flask(__name__)
@app.route('/send',methods = ['GET','POST'])
def send():
if request.method == 'POST':
age = request.form['age']
return render_template('age.html',age=age)
return render_template('index.html')
if __name__ == '__main__':
app.run()
这是index.html文件
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
</head>
<body>
<h1>How old are you?</h1>
<form method="POST" action = "/send">
<div class = "form-group">
<input type="text" name = "age">
</div>
<input class="btn btn-primary" type="submit" value="submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Your age is{{age}}</h1>
</body>
你多大了?
这是age.html文件
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
</head>
<body>
<h1>How old are you?</h1>
<form method="POST" action = "/send">
<div class = "form-group">
<input type="text" name = "age">
</div>
<input class="btn btn-primary" type="submit" value="submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Your age is{{age}}</h1>
</body>
你的年龄是{{age}
试试这里:
如果这不起作用,您的控制台或浏览器中会出现什么错误
编辑:
我只是试了一下,效果很好。尝试在浏览器中的新选项卡中重新加载该页面,然后查看是否仍然存在。此错误与您在网页上输入的命名以及使用
请求为表单编制索引的方式有关。表单['age']
您没有设置根路径
还是你开
或者,您可以使用这个快速而肮脏的修复程序(正如您在decorators中看到的,现在同时考虑了/and/send):
如果不处理“/”路由,则在打开时不会发生任何事情
如果你能更清楚地解释一下你想要得到什么样的结果,我会帮你做得更好。在寻找我试图解决的问题时,我发现了你的问题。对于包含完整代码(带有py和2个HTML文件)的解决方案,您可以查看my repl.it: 问题是:
从烧瓶导入烧瓶,呈现模板,请求
app=烧瓶(名称)
@app.route('/',方法=['GET','POST'])
def send():
如果request.method==“POST”:
年龄=请求。表格['age']
返回渲染模板('age.html',age=age)
返回渲染模板('index.html')
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
运行(主机='0.0.0.0',端口=8080
我将主机保留为“0.0.0.0”,端口保留为8080。如果在终端上运行Python,可以尝试(主机='127.0.0.1',端口:5000')
对于模板/index.html
你多大了?
对于模板/age.html
你的年龄是{{age}
请求不正确我转到了,但出现以下错误:浏览器(或代理)发送了一个此服务器无法理解的请求。我编写了后端python脚本,希望在用户输入上运行,并将获得的值返回给用户。上面的代码是我希望执行的操作的简化版本,旨在询问用户的年龄并将其打印回。即使您在代码:内部服务器错误服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序中存在错误。代码在我的电脑上运行正常。这可能是一个简单的缓存问题吗?您是否尝试删除缓存或使用其他浏览器?您在python终端窗口中看到了什么TemplateNotFound:index.html 127.0.0.1---[18/Apr/2018 20:42:04]“获取/发送HTTP/1.1”500-您是否将index.html放入templates文件夹(在脚本路径中创建)?也许您可以在stackoverflow中插入回复,以避免插入依赖项。