How can I override static file handler in Flask?
Specifically, how can I assign a new handler to a "static" endpoint? I know I can change static_folder
and static_path
, but I specifically want to assign a different function to handle any url requests that are redirected to a "static" endpoint in the routing map. I've tried assigning empty werkzeug.routing.Map
to <Flask app>.url_map
, to no avail - I still get an error (" View function mapping is overwriting an existing endpoint function: static
") when I try add_url_rule
.
Thanks in advance.
source to share
Set static_folder
to None
to prevent the registration of type box:
app = Flask(static_folder=None)
Now you can create your own.
Alternatively, the view static
uses a different URL path and provide an alternative for a different endpoint name:
app = Flask(static_url_path='/flask_static')
@route('/static/<path:filename>')
def my_static(filename):
# ...
The checkbox will always use the endpoint name static
for the view it generates, so the above example uses my_static
.
source to share