【问题标题】:Flask can't find template when specifying templates folder [duplicate]指定模板文件夹时Flask找不到模板[重复]
【发布时间】:2017-08-13 16:10:39
【问题描述】:

我在我的 Flask 应用程序旁边的模板文件夹中创建了一个模板。当我尝试渲染此模板时,我在浏览器中收到 404 错误。为什么我不能渲染模板?

from flask import Flask, render_template

app = Flask(__name__, template_folder='/templates')

@app.route('/')
def index():
    return render_template('index.html')

【问题讨论】:

  • 从template_folder='/templates' 中删除斜线。它应该看起来像 template_folder='templates'。
  • 感谢大家的帮助!不,这是一个 404 错误。如果你愿意,我可以提供截图作为证据。我在 template_folder 和 template_folder 本身中添加了斜线,因为我在这里找到了另一个主题,当时我已经在寻找答案。
  • 好的。为什么flask找不到模板,即使没有template_folder部分或部分编辑为template_folder='templates'?

标签: python flask


【解决方案1】:

您无需为应用指定模板文件夹,它会自动设置为'templates'。正如该默认值所示,它是一个相对路径。

您提供了一个绝对路径,/templates 是“文件系统根目录下的模板文件夹”。您的机器不太可能在根目录下包含模板文件夹。

去掉template_folder参数,或者去掉前面的/。

【讨论】:

    猜你喜欢
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多