gpt4 book ai didi

python - 在 Flask 中覆盖 send_static_file()

转载 作者:太空宇宙 更新时间:2023-11-04 01:23:24 27 4
gpt4 key购买 nike

我正在尝试使用 this answer我的问题,但我不知道如何正确使用它。

我的代码:

app = Flask(名称)

app = Flask(__name__, static_folder="static", static_path="")

class SecuredStaticFlask(app):
def send_static_file(self, filename):
# Get user from session
if current_user.is_authenticated():
return super(SecuredStaticFlask, self).send_static_file(filename)

else:
abort(403)

当我在浏览器中访问 http://localhost:5000/some_existing_file 时,Flask 报错:

Traceback (most recent call last):
File "/home/honza/Stažené/pycharm-community-3.0/helpers/pydev/pydevd.py", line 1498, in <module>
debugger.run(setup['file'], None, None)
File "/home/honza/Stažené/pycharm-community-3.0/helpers/pydev/pydevd.py", line 1139, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "/home/honza/workspace/web_development/login/toneid_web_api_jk.py", line 37, in <module>
class SecuredStaticFlask(app):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 507, in __init__
self.add_url_rule(self.static_url_path + '/<path:filename>',
TypeError: Error when calling the metaclass bases
can only concatenate tuple (not "str") to tuple

flask.Flask 的实例是app,对吧?所以,我认为 Flask 的子类是 subclass_name(parent_class)

有人能帮帮我吗?谢谢。

最佳答案

在我看来应该是:

class SecuredStaticFlask(Flask):
def send_static_file(self, filename):
# Get user from session
if current_user.is_authenticated():
return super(SecuredStaticFlask, self).send_static_file(filename)

else:
abort(403)

app = SecuredStaticFlask(__name__, static_folder="static", static_path="")

关于python - 在 Flask 中覆盖 send_static_file(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19765473/

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