gpt4 book ai didi

python - Flask render_template 上下文在蓝图中为 None

转载 作者:行者123 更新时间:2023-12-03 00:08:25 25 4
gpt4 key购买 nike

我对 Flask 比较陌生,但我已经发现需要使用蓝图。但是,在我的蓝图中,我尝试渲染模板,但出现错误。

当连接为 WSGI 应用程序(在 Dreamhost 上)时,render_template 函数返回此错误:

File ".../app/ui/__init__.py", line 95, in index
response = make_response(render_template('index.html', **data))

File ".../flask/templating.py", line 123, in render_template
ctx.app.update_template_context(context)

AttributeError: 'NoneType' object has no attribute 'app'

但是,当我在 Debug模式下直接调用 app.py 时,它工作得很好! (下)

python app/app.py

在app.py中:

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

编辑:渲染模板:

@ui_blueprint.route('/', methods=['GET'])
@ui_blueprint.route('/home', methods=['GET'])
def index():
data = {
'title': 'Index'
}
response = make_response(render_template('index.html', **data))
return response

编辑2: ctx是:

  • None在 WSGI 应用案例中
  • <RequestContext 'http://aaa.bbb.com:5000/' [GET] of __init__>在直接调用的情况下

有什么想法可以解决这个错误吗?谢谢!

最佳答案

确保您导入所有正确的模块:

从 Flask 导入 Flask、render_template、make_response、请求、响应

您可能想为您的 View 创建一个包装器。

关于python - Flask render_template 上下文在蓝图中为 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13005476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com