【问题标题】:How do I find my template using Flask Blueprints?如何使用 Flask 蓝图找到我的模板?
【发布时间】:2015-03-24 18:12:15
【问题描述】:

我已经搜索了我可以在 SO 上找到的所有内容,包括 flask blueprint template folder

/root
 __init__.py
 run.py
    /hrms
      __init__.py
      views.py
      models.py
    /static
    /templates
        __init__.py
        layout.html
        /hr
            __init__.py
            hr_template_test.html

我也试过了:

/hrms
  __init__.py
  views.py
  models.py
  /templates
      __init__.py
      hr_template_test.html

无论我把hr_template_test.html 文件放在哪里,Flask 都找不到它。这是我在__init__.py 中的内容:

from flask import Blueprint, render_template

hrms_blue = Blueprint('hrms', __name__, template_folder='templates')

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

run.py:

from flask import Flask
from flask_debugtoolbar import DebugToolbarExtension
from application.views.hrms import hrms_blue

app = Flask(__name__)
app.register_blueprint(hrms_blue, url_prefix='/<hrms_id>')

我试过了:

return render_template('hr/hrms_data_view.html')
return render_template('application/templates/hr/hrms_data_view.html')
return render_template('templates/hr/hrms_data_view.html')
return render_template('hrms_data_view.html')

http://127.0.0.1/hrms

Jinja2 抱怨找不到 html 文件。

我从模板和 hr 中取出了__init__.py。不,我明白了,TypeError: index() takes no arguments (1 given)

【问题讨论】:

  • 您的模板文件夹中不应包含__init__.py 文件。它不是一个包。
  • 我试过没有它。我会再试一次。
  • @MartijnPieters 我把它拿出来,得到了 TypeError: index() 没有参数(给定 1 个)。我已经这样做了十亿次,所以我可能忘了我把它拿出来放回去了。无论如何,我得到了新的错误。 (我在这里查看了您的许多答案,仍在阅读它们。)
  • 您的蓝图采用路由参数,但您的视图不接受它。
  • @MartijnPieters 请告诉我这意味着什么。

标签: python-2.7 flask


【解决方案1】:

您可能遇到循环导入问题。 你应该尝试这样做:

def register(app):
    from __init__ import hrms_blue
    app.register_blueprint(hrms_blue)

register(app)

而不是做:

app.register_blueprint(hrms_blue, url_prefix='/<hrms_id>')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 2015-09-06
    • 1970-01-01
    相关资源
    最近更新 更多