Flask url_for incorrectly links to static pages outside of templates Flask url_for incorrectly links to static pages outside of templates flask flask

Flask url_for incorrectly links to static pages outside of templates


url_for returns an url path. Not a filesystem path.

So you're trying to save your file at /static/something which, for the system, means a path starting from the root of your filesystem, not your application path.

You can create the static path for your file with something like this

static_path_to_save = os.path.join([app.root_path, '/static', fn])

Just a side note, when dealing with uploads, remember to sanityze all the paths and to double check the destination. Best practices applies, like stripping slashes if you use the filename provided by the user (best is if you generate the filename).

In your code I see also a problem if 2 users uploads a file with the same name, overwriting each other. But this is probably safe in your context.