Flask sets up the route for static files using your static path (defaults to /static
) and any text. From the source:
self.add_url_rule(self.static_url_path + '/<path:filename>',
endpoint='static',
view_func=self.send_static_file)
send_to_static
passes whatever this route assigns to filename
.
return send_from_directory(self.static_folder, filename,
cache_timeout=cache_timeout)
If you look at the source for send_from_directory
you will see that it just uses safe_join(directory, filename)
to get the path to the file.
safe_join
itself just does some work to normalize the path and watch out for things like ..
in the filename.
Going back to the URL route, path
matches any text. It's like string
except it accepts slashes. This means that you can use any level of nesting within your static folder. URLs like /static/file.txt
and /static/p/a/t/h/t/o/file.txt
will all work. So long as the URLs begin with your static path, the behavior you want works out of the box:
http://localhost:5000/static/css/images/image1.png