程序部署到服务器报错:jinja2.exceptions.TemplateNotFound:base.html

博客程序在windows本地测试正常。部署到ubuntu python3.5环境时登录和注册页面报错:
```

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/app.py”, line 2309 , in __call__

return self.wsgi_app(environ, start_response)

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/app.py”, line 2295 , in wsgi_app

response = self.handle_exception(e)

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/app.py”, line 1741 , in handle_exception

reraise(exc_type, exc_value, tb)

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/_compat.py”, line 35 , in reraise

raise value

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/app.py”, line 2292 , in wsgi_app

response = self.full_dispatch_request()

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/app.py”, line 1815 , in full_dispatch_request

rv = self.handle_user_exception(e)

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/app.py”, line 1718 , in handle_user_exception

reraise(exc_type, exc_value, tb)

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/_compat.py”, line 35 , in reraise

raise value

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/app.py”, line 1813 , in full_dispatch_request

rv = self.dispatch_request()

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/app.py”, line 1799 , in dispatch_request

return self.view_functionsrule.endpoint

  • File “/root/blog/app/views/auth.py”, line 57 , in register

return render_template(‘register.html’, form=form)

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/templating.py”, line 135 , in render_template

context, ctx.app)

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/templating.py”, line 117 , in _render

rv = template.render(context)

  • File “/root/blog/venv/lib/python3.5/site-packages/jinja2/environment.py”, line 1008 , in render

return self.environment.handle_exception(exc_info, True)

  • File “/root/blog/venv/lib/python3.5/site-packages/jinja2/environment.py”, line 780 , in handle_exception

reraise(exc_type, exc_value, tb)

  • File “/root/blog/venv/lib/python3.5/site-packages/jinja2/_compat.py”, line 37 , in reraise

raise value.with_traceback(tb)

  • File “/root/blog/app/templates/register.html”, line 1 , in top-level template code

{% extends 'base.html '%}

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/templating.py”, line 58 , in get_source

return self._get_source_fast(environment, template)

  • File “/root/blog/venv/lib/python3.5/site-packages/flask/templating.py”, line 86 , in _get_source_fast

raise TemplateNotFound(template)

jinja2.exceptions.TemplateNotFound: base.html



。而首页和文章页可以正常访问。  ip地址:http://47.103.12.42/。

请为代码块设置语法高亮,使用编辑器上面的按钮,或是参考论坛说明里的编辑器使用一节

login.html 没有找到 base.html,你可以设置EXPLAIN_TEMPLATE_LOADING = True配置看看jinja2加载步骤:

# setting.py

class Config(object):
    # If you encounter problems loading the correct templates enable the EXPLAIN_TEMPLATE_LOADING config variable which will instruct Flask to print out the steps it goes through to locate templates on every render_template call.
    # http://flask.pocoo.org/docs/1.0/blueprints/#templates
    EXPLAIN_TEMPLATE_LOADING = True
1 个赞

已配置该命令,报错如下:

[2019-03-30 12:21:48,554] INFO in debughelpers: Locating template "register.html":
    1: trying loader of application "app"
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /root/blog/app/templates
       -> found ('/root/blog/app/templates/register.html')
    2: trying loader of blueprint "bootstrap" (flask_bootstrap)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /root/blog/venv/lib/python3.5/site-packages/flask_bootstrap/templates
       -> no match
[2019-03-30 12:21:48,560] INFO in debughelpers: Locating template "base.html ":
    1: trying loader of application "app"
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /root/blog/app/templates
       -> no match
    2: trying loader of blueprint "bootstrap" (flask_bootstrap)
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /root/blog/venv/lib/python3.5/site-packages/flask_bootstrap/templates
       -> no match
Error: the template could not be found.
  The template was looked up from an endpoint that belongs to the blueprint "auth".
  Maybe you did not place a template in the right folder?
  See http://flask.pocoo.org/docs/blueprints/#templates
120.229.144.232 - - [30/Mar/2019 12:21:48] "GET /auth/register HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/root/blog/venv/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/root/blog/venv/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/root/blog/venv/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/root/blog/venv/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/root/blog/venv/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/root/blog/venv/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/root/blog/venv/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/root/blog/venv/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/root/blog/venv/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/root/blog/venv/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/root/blog/app/views/auth.py", line 57, in register
    return render_template('register.html', form=form)
  File "/root/blog/venv/lib/python3.5/site-packages/flask/templating.py", line 135, in render_template
    context, ctx.app)
  File "/root/blog/venv/lib/python3.5/site-packages/flask/templating.py", line 117, in _render
    rv = template.render(context)
  File "/root/blog/venv/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/root/blog/venv/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/root/blog/venv/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/root/blog/app/templates/register.html", line 1, in top-level template code
    {% extends 'base.html '%}
  File "/root/blog/venv/lib/python3.5/site-packages/flask/templating.py", line 57, in get_source
    return self._get_source_explained(environment, template)
  File "/root/blog/venv/lib/python3.5/site-packages/flask/templating.py", line 78, in _get_source_explained
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: base.html

猜测是 Flask-Bootstrap 和 Bootstrap-Flask 混用导致的。前者内置了基模板,后者没有。

需要更多信息,麻烦提供你自己创建的基模板开始几行和模板目录文件结构。

楼主还说「windows本地测试正常,部署到Ubuntu报错」,我估计是本地开发配置和部署配置有点差异。虽然报错信息看起来像是第三方包丢失base.html文件,但这个应该不可能的。

只用了bootstrap-flask 我直接把base文件添加到 报错的页面,最笨也是最直接。:grinning: 让bug见鬼吧