Default location:
Description: Most Web applications will provide a CDN-based static file service to give users a better experience, static files mainly include css/js/Pictures/fonts, etc., flask by default only need to create "static" directory under the project root directory, the project uses based on/ The path to the static start can be accessed, but for better processing power, it is recommended to use Nginx or another Web server to manage static files
Recommended Solutions:
1. Do not write the static file path directly in the project, you should use Url_for to generate the path, of course, if we want to specify the path can also specify the Static_folder parameter when creating the app instance
#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import public module from Flask import flask, url_for, jsonify# Description: Import other modules app = Flask (__name__, static_folder= ' static ') @app. Rout E ('/') def index (): Return jsonify (url_for (' Static ', filename= ' img/girl.jpg ', _external=true)) if __name__ = = ' __main__ ' : App.run (host= ' 0.0.0.0 ', port=9000, Debug=true)
Note: The static_folder specified when creating an app instance above is relative to the root directory (Flask.helpers.get_root_path), supports any specified static file directory, Url_for () The first parameter static when generating the URL is actually the default view function in App.url_map, so it can be passed directly, filename is the parameter that the static view function must have, and the relative URL path is returned by default, if a full URL path is required under some special scenarios ( such as registration confirmation email) can return full URL path by specifying _external=true
This article is from "@Can up and No BB ..." Blog, be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1866524
Site backend _python+flask.0009.flask The default and custom resource directory for static resources?