【发布时间】:2020-06-05 04:02:10
【问题描述】:
我正在开发基于 Flask 的 Web 应用程序。当我在 localhost:5000/ 上运行我的 app.py 时,它适用于前三页。我可以导航主页>用户>查看用户。但是当我从 viewuser 启动 hub.html 文件时,jinja 抛出了 TemplateNotFound 错误。 我确实有一个模板文件夹。其他网页从同一文件夹呈现。为什么只在这个页面抛出异常?
app.py 中调用的函数:
@app.route("/hub")
def hub():
users = service.pullusers()
return render_template('hub', ulist=users)
viewuser.html 中的调用函数
<div>
<br><br><br><a class="btn btn-primary btn-xl" href={{ url_for('hub')}}>Transfer Credits</a>
</div>
文件树:
C:.
│ a.txt
│ app.py
│ main.css
│ models.py
│ service.py
│
├───static
│ ├───css
│ │ custom-responsive-styles.css
│ │ main.css
│ │ styles.css
│ │
│ └───js
│ all-plugins.js
│ jquery-3.2.1.min.js
│ plugins-activate.js
│
├───templates
│ home.html
│ hub.html
│ sendcreds.html
│ users.html
│ viewuser.html
│
└───__pycache__
models.cpython-37.pyc
service.cpython-37.pyc
错误:
jinja2.exceptions.TemplateNotFound
jinja2.exceptions.TemplateNotFound: hub
Traceback (most recent call last)
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\samar\Documents\TSF\app.py", line 26, in hub
return render_template('hub', ulist=users)
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\templating.py", line 138, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2\environment.py", line 930, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2\environment.py", line 883, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2\environment.py", line 857, in _load_template
template = self.loader.load(self, name, globals)
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2\loaders.py", line 115, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\templating.py", line 60, in get_source
return self._get_source_fast(environment, template)
File "C:\Users\samar\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\templating.py", line 89, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: hub
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
Brought to you by DON'T PANIC, your friendly Werkzeug powered traceback interpreter.
我做错了什么?请快帮我。我很高兴。
【问题讨论】:
-
...
r'C:/templates/hub.html'?此外,请确保您的工作目录位于您认为的位置。import os; print(os.getcwd())确认。
标签: python html python-3.x flask jinja2