为什么 Flask 不使用下面的代码呈现 html 页面?
Why Flask is not rendering the html page using code below?
我在 AWS C9
平台上启动了一个新项目,但我在使用框架 Flask 呈现网页时遇到问题
框架正常运行它可以 return 消息(默认情况下)但是当我尝试 return 模板(Html 页面)时我收到错误:"TemplateNotFound"
我将在下面留下代码和错误消息
import os
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route("/")
@app.route("/home")
def home():
return render_template('templates/home.html')
@app.route("/about")
def about():
return "<h1>Flask is working</h1>"
if __name__=="__main__":
app.run(host='0.0.0.0', port=8080)
app.debug = True
堆栈跟踪:-
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2311, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1834, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1737, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1832, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1818, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ec2-user/environment/Flask_Catalog/flaskCatalog.py", line 10, in home
return render_template('templates/home.html')
File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 134, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "/usr/local/lib/python2.7/site-packages/jinja2/loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 58, in get_source
return self._get_source_fast(environment, template)
File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 86, in _get_source_fast
raise TemplateNotFound(template)
TemplateNotFound: templates/home.html
LicentaCatalogOnline2019
|
+--Flask_Catalog
| |
| +-- flaskCatalog.py
|
+--templates
| |
| +-- about.html
| +-- home.html
默认情况下,flask 在 templates
文件夹中查找 html 模板。您不需要在 return 语句中明确附加 /templates
。
使用这个
return render_template('home.html')
我在 AWS C9
平台上启动了一个新项目,但我在使用框架 Flask 呈现网页时遇到问题
框架正常运行它可以 return 消息(默认情况下)但是当我尝试 return 模板(Html 页面)时我收到错误:"TemplateNotFound" 我将在下面留下代码和错误消息
import os
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route("/")
@app.route("/home")
def home():
return render_template('templates/home.html')
@app.route("/about")
def about():
return "<h1>Flask is working</h1>"
if __name__=="__main__":
app.run(host='0.0.0.0', port=8080)
app.debug = True
堆栈跟踪:-
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2311, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1834, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1737, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1832, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1818, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ec2-user/environment/Flask_Catalog/flaskCatalog.py", line 10, in home
return render_template('templates/home.html')
File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 134, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "/usr/local/lib/python2.7/site-packages/jinja2/loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 58, in get_source
return self._get_source_fast(environment, template)
File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 86, in _get_source_fast
raise TemplateNotFound(template)
TemplateNotFound: templates/home.html
LicentaCatalogOnline2019
|
+--Flask_Catalog
| |
| +-- flaskCatalog.py
|
+--templates
| |
| +-- about.html
| +-- home.html
默认情况下,flask 在 templates
文件夹中查找 html 模板。您不需要在 return 语句中明确附加 /templates
。
使用这个
return render_template('home.html')