无法在python中加载模板

时间:2018-10-11 10:23:26

标签: python flask internal-server-error

烧瓶在render_template()方法上给模板找不到异常

enter image description here

routes.py

from flask import render_template
from app import app

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html', title='Home', user='user')
    # return render_template('index.html')

index.html

<html>
    <head>
        <title>{{ title }} </title>
    </head>
    <body>
        <h1>Hello, {{ user }}!</h1>
    </body>
</html>

初始化 .py

from flask import Flask
app=Flask(__name__)
from app import routes

bot.py

from app import app

我收到的错误日志

[2018-10-11 15:00:25,349] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "D:\Bot\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "D:\Bot\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "D:\Bot\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "D:\Bot\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "D:\Bot\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "D:\Bot\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\Bot\app\routes.py", line 8, in index
    return render_template('index.html')
  File "D:\Bot\venv\lib\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 "D:\Bot\venv\lib\site-packages\jinja2\environment.py", line 869, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "D:\Bot\venv\lib\site-packages\jinja2\environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "D:\Bot\venv\lib\site-packages\jinja2\environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "D:\Bot\venv\lib\site-packages\jinja2\loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "D:\Bot\venv\lib\site-packages\flask\templating.py", line 58, in get_source
    return self._get_source_fast(environment, template)
  File "D:\Bot\venv\lib\site-packages\flask\templating.py", line 86, in _get_source_fast
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: index.html
127.0.0.1 - - [11/Oct/2018 15:00:25] "GET / HTTP/1.1" 500 -

我已经尝试过回答this问题,但是仍然无法正常工作。

有人帮我解决这个问题,谢谢...

1 个答案:

答案 0 :(得分:0)

您需要将myuser ALL=(ALL) NOPASSWD:/bin/pkill myprogram 目录移至templates目录。

为什么?

因为app目录与应用程序(Flask的实例)位于同一目录中的文件相同。

templates

在Flask中,jinja2环境依靠 app = Flask(__name__) 参数来加载模板。