jinja2.exceptions.TemplateNotFound 错误 [重复]

Posted

技术标签:

【中文标题】jinja2.exceptions.TemplateNotFound 错误 [重复]【英文标题】:jinja2.exceptions.TemplateNotFound error [duplicate] 【发布时间】:2013-02-09 19:38:57 【问题描述】:

我使用烧瓶,当我调用此 url 时出现此错误:/login 这是我的登录方法:

@app.route('/login')
def login():
    if authenticateForPanel():
        return redirect(url_for("panel"))
    else:
        getParam = request.args.getlist('redirect_uri')
        if getParam:
            ref =getParam[0]
        else:
            ref="/panel"
        return render_template( themesDir + g.blogOptions['active_theme']+'/login.html', blogOptions = g.blogOptions, ref=ref )

还有回溯:

Traceback (most recent call last):
  File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/ozcan/Documents/python/app.py", line 209, in login
    return render_template( themesDir + g.blogOptions['active_theme']+'/login.html', blogOptions = g.blogOptions, ref=ref )
  File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/templating.py", line 124, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/jinja2/environment.py", line 758, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/jinja2/environment.py", line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/jinja2/environment.py", line 693, in _load_template
    template = self.loader.load(self, name, globals)
  File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/jinja2/loaders.py", line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/templating.py", line 61, in get_source
    raise TemplateNotFound(template)
TemplateNotFound: static/themes/default/login.html

我绝对确定 login.html 在那里(static/themes/default/404.html)。为什么会发生这种情况?

【问题讨论】:

【参考方案1】:

您将模板放在错误的位置。来自 Flask 文档:

Flask 将在模板文件夹中查找模板。所以如果你的应用程序是一个模块,这个文件夹就在那个模块旁边,如果它是一个包,它实际上在你的包里面: 有关更多信息,请参阅文档:http://flask.pocoo.org/docs/quickstart/#rendering-templates

【讨论】:

【参考方案2】:

我认为你不应该在前面加上 themesDir。您只需将模板的文件名传递给烧瓶,然后它会在相对于您的 python 文件名为 templates 的文件夹中查找。

【讨论】:

不,它不起作用,错误说文件所在的目录完全相同 你不应该把你的主题放在static文件夹中!否则每个人都可以下载您的模板! 我的目的是,资产文件夹必须在每个主题中。我无法用其他任何方式做到这一点。这是我的问题:***.com/questions/15027930/…

以上是关于jinja2.exceptions.TemplateNotFound 错误 [重复]的主要内容,如果未能解决你的问题,请参考以下文章