【问题标题】:unable to load template in python [duplicate]无法在python中加载模板[重复]
【发布时间】:2018-10-11 10:23:25
【问题描述】:

Flask 在render_template() 方法上给出模板未找到异常

.

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问题的答案,但还是不行。

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

【问题讨论】:

  • templates 文件夹和 routes.py 应该在同一个文件夹中。将模板文件夹粘贴到您的应用文件夹中
  • 谢谢哥们...!那行得通。由于我是python新手,所以我不知道这样的事情。再次感谢。

标签: python flask internal-server-error


【解决方案1】:

您需要将templates 目录移动到app 目录。

为什么?

因为templates目录和app(Flask的实例)所在的文件在同一个目录下。

app = Flask(__name__)

在 Flask 内部,jinja2 环境依赖 __name__ 参数来加载您的模板。

【讨论】:

    猜你喜欢
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 2015-11-10
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多