Flask: serve assets without leading slash using url_for

江枫思渺然 提交于 2019-12-03 09:08:52

I was having a similar issue with loading a css file that was on a custom static path. One fix could be to change the ROOT_DIRECTORY of the application, but this didn't work for my application as I only need to change the static path.

I used a combination of static_folder and static_url_path:

STATIC_URL_PATH = '/your/custom/path/static' # Where the css is stored
STATIC_FOLDER = 'your/custom/path/static'

app = Flask(__name__, static_folder=STATIC_FOLDER,
            static_url_path=STATIC_URL_PATH)

As you can see, the main difference is the leading / in the beginning, but this made the app to be able to find the css.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!