我试图在Google上使用Flask渲染一个html模板,但是虽然看起来很简单,但我无法这样做。我正在做的事情是:
from flask_ngrok import run_with_ngrok
from flask import Flask, render_template , request
from google.colab import drive
drive.mount('/content/gdrive')
app = Flask(__name__)
run_with_ngrok(app)
@app.route('/')
def home():
return render_template('/content/gdrive/MyDrive/test/test.html')
if __name__ == '__main__':
app.run()我的html文件看起来如下(它只是一个测试文件)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Talk to the bot</title>
</head>
<body>
</body>
</html>我得到了一个内部服务器错误。以下是错误的完整输出:
* Serving Flask app "__main__" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Running on http://3b510d3973eb.ngrok.io
* Traffic stats available on http://127.0.0.1:4040
[2021-02-03 15:29:42,269] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "<ipython-input-32-84fc2b6eea6b>", line 7, in home
return render_template('/content/gdrive/MyDrive/test/test.html')
File "/usr/local/lib/python3.6/dist-packages/flask/templating.py", line 138, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/local/lib/python3.6/dist-packages/jinja2/environment.py", line 930, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/local/lib/python3.6/dist-packages/jinja2/environment.py", line 883, in get_template
return self._load_template(name, self.make_globals(globals))
File "/usr/local/lib/python3.6/dist-packages/jinja2/environment.py", line 857, in _load_template
template = self.loader.load(self, name, globals)
File "/usr/local/lib/python3.6/dist-packages/jinja2/loaders.py", line 115, in load
source, filename, uptodate = self.get_source(environment, name)
File "/usr/local/lib/python3.6/dist-packages/flask/templating.py", line 60, in get_source
return self._get_source_fast(environment, template)
File "/usr/local/lib/python3.6/dist-packages/flask/templating.py", line 89, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: /content/gdrive/MyDrive/test/test.html
127.0.0.1 - - [03/Feb/2021 15:29:42] "GET / HTTP/1.1" 500 -
127.0.0.1 - - [03/Feb/2021 15:29:42] "GET /favicon.ico HTTP/1.1" 404 -我知道一定有一个简单的错误发生在某个地方,但由于我对这类事情并不熟悉,所以我无法理解。谢谢
发布于 2021-02-03 16:05:02
通常,render_template需要相对路径,并在子文件夹templates中搜索它。
app.py # file with Flask code
templates/test.html和
return render_template('test.html')如果要使用绝对路径,则必须设置root文件夹-- / -和带有模板的文件夹。
app = Flask(__name__, template_folder='/')https://stackoverflow.com/questions/66030868
复制相似问题