flask:jinja2.exceptions.TemplateNotFound错误

时间:2017-11-03 21:02:58

标签: python flask jinja2

我正在为一个项目制作一个网站但我继续收到这个错误。

jinja2.exceptions.TemplateNotFound:home.html

然后我明白了:

File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\J\Desktop\hello.py", line 5, in home
return render_template('home.html')
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\templating.py", line 133, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\templating.py", line 57, in get_source
return self._get_source_fast(environment, template)
File "C:\Users\J\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\templating.py", line 85, in _get_source_fast
raise TemplateNotFound(template)

这里也是我的代码

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
    return render_template('home.html')
@app.route('/about/')
def about():
    return render_template('about.html')
if __name__ == '__main__':
    app.run(debug=True)

2 个答案:

答案 0 :(得分:4)

您需要确保模板html文件位于名为templates的子目录中。

如果它们位于那里,它们都会起作用。

有关render_template的简单说明,请参阅flask quickstart

答案 1 :(得分:1)

好像你的html文件不在同一个目录中。尝试阅读该文件,看看是否可以打开它。

with open('home.html', 'r') as html_file:
    html= html_file.read()

或尝试这样做

import os.path
os.path.isfile('home.html') 

您的目录结构应该如下所示

/home.py
/templates
    /home.html