在Flask中覆盖send_static_file()

时间:2013-11-04 10:09:31

标签: python flask

我正在尝试使用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)

任何人都可以帮助我吗?感谢。

1 个答案:

答案 0 :(得分:2)

在我看来,它应该符合以下几行:

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="")