【问题标题】:Python -jinja2.exceptions.TemplateNotFound: base.html errorPython -jinja2.exceptions.TemplateNotFound:base.html 错误
【发布时间】:2020-08-22 03:24:59
【问题描述】:

所以我用 Flask 创建了一个博客,当我在本地运行它时,它工作得很好。但是在日志中的 heroku 部署中,我收到“raise TemplateNotFound(template)”错误。

run.py

from flaskblog import app

if __name__ == '__main__':
    app.run()

__init__.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__, template_folder="templates")
app.config['SECRET_KEY'] = '1238944784'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)

from flaskblog import routes

过程文件

web: gunicorn run:app

----flaskblog (Directory)
        Procfile
        requirements.txt
        run.py
--------flaskblog (Directory)
            forms.py
            models.py
            routes.py
            site.db
            __init__.py
------------static (Directory)
                main.css
------------templates (Directory)
                admin.html
                base.html
                edit.html
                home.html
                post.html
                posts.html

在 heroku 日志中,我收到“jinja2.exceptions.TemplateNotFound: base.html”错误。

这里也是项目的 Github 存储库:Github Repository

【问题讨论】:

  • 不加template_目录可以试试吗
  • 我相信你的意思是 template_folder="templates"。是的,我也尝试过,实际上我添加它是为了解决问题。但它仍在进行中。
  • 我已经添加了答案

标签: python flask heroku jinja2


【解决方案1】:

您的每个 html 文件中都有一个简单的拼写错误。

一些 html 文件顶部有此代码以从 base.html 扩展,但末尾有一个空格

你是这样写的

{% extends "base.html "%}

你应该把最后的空格去掉,改成这个

{% extends "base.html"%}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2019-08-10
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    相关资源
    最近更新 更多