# -*- coding:utf-8 -*-from flask import Flask, url_forapp1 = Flask(__name__, static_folder=‘mystatic‘, static_url_path=‘/myurl‘, template_folder=‘mytemplate‘)app2 = Flask(__name__)app3 = Flask(__name__, static_url_path=‘‘)@app1.route(‘/‘)def hello_world(): return ‘Hello World!‘@app1.route("/css")def static_css(): return url_for(‘static‘, filename=‘style.css‘)@app1.route("/js")def static_js(): return url_for(‘static‘, filename=‘login.js‘)# @app.route("/ss")# def static():# return "hello ss!"# AssertionError: View function mapping is overwriting an existing endpoint function: static# 不允许重复定义内部约束方法staticif __name__ == ‘__main__‘: print("1--------------------") print(app1.__dict__) print app1.view_functions[‘static‘].__dict__ print("2--------------------") print app2.__dict__ print("3--------------------") print app3.__dict__ # app1.run() # app2.run(port=5001) # app3.run(port=5002)
Directory structure
flask_simply_dome -mystatic --login.js -->"this is a test login.js" -static --login.js -->"this is app3 or app2 login.js" asimply_app.py
# curl "http://127.0.0.1:5000/css"/myurl/style.css# curl "http://127.0.0.1:5000/js"/myurl/login.js# curl "http://127.0.0.1:5000/myurl/login.js""this is a test login.js"# curl "http://127.0.0.1:5001/static/login.js""this is app3 or app2 login.js"# curl "http://127.0.0.1:5002/login.js""this is app3 or app2 login.js"
Compare the properties of each app
1--------------------{' subdomain_matching ': False, ' Error_handler_spec ': {}, ' _before_request_lock ': < thread . Lock object at 0X0000000002619D50 >, ' Before_request_funcs ': {}, ' Teardown_appcontext_funcs ': [], ' Shell_con Text_processors ': [], ' After_request_funcs ': {}, ' CLI ': < Flask.cli.AppGroup object at 0x0000000002ef0278 >, ' _blueprint_order ': [], ' Before_first_request_funcs ': [], ' view_functions ': {' static_css ': < function Static_css at 0x0000000002ef87b8 >, ' static_js ': < function static_js at 0x0000000002ef8828 >, ' H Ello_world ': < function Hello_world at 0x0000000002ef8748 >, ' static ': < bound method Flask.send_static_f Ile of < Flask ' Asimply_app ' >>}, ' Instance_path ': ' d:\\myself\\flask_simply_dome\\instance ', ' teardown _request_funcs ': {}, ' Url_value_preprocessors ': {}, ' config ': < config {' json_as_ascii ': True, ' US E_x_sendfile ': False, ' Session_cookie_secure ': False, ' Session_cookie_path ': None, ' Session_cookie_domain ': none, ' SES Sion_cookie_name ': ' Session ', ' max_cookie_size ': 4093, ' session_cookie_samesite ': None, ' propagate_ex Ceptions ': None, ' ENV ': ' Production ', ' DEBUG ': False, ' Secret_key ': none, ' Explain_template_lo Ading ': False, ' max_content_length ': None, ' application_root ': '/', ' server_name ': none, ' PREF Erred_url_scheme ': ' http ', ' jsonify_prettyprint_regular ': false, ' testing ': false, ' permanent_session _lifetime ': Datetime.timedelta (+), ' templates_auto_reload ': None, ' trap_bad_request_errors ': none, ' Json_sort_keys ': True, ' jsonify_mimetype ': ' Application/json ', ' session_cookie_httponly ': true, ' SEND _file_max_age_default ': Datetime.timedelta (0, 43200), ' preserve_context_on_exception ': None, ' session_refres H_each_request ': True, ' Trap_http_exceptions ': False} >, ' _static_url_path ': '/myurl ', ' template_context_processors ': { None: [< function _default_template_ctx_processor at 0x0000000002ed9ba8 >]}, ' Template_folder ': ' Mytempla Te ', ' Blueprints ': {}, ' Url_map ': Map ([< Rule '/css ' (HEAD, OPTIONS, GET)-> Static_css >, < R Ule '/js ' (head, Options, get)-> Static_js >, < Rule '/' (head, Options, get)-> Hello_world >, < Rule '/myurl/<filename> ' (HEAD, OPTIONS, GET)-> Static >]), ' name ': ' Asimply_app ', ' _ Got_first_request ': False, ' import_name ': ' __main__ ', ' root_path ': ' D:\\myself\\flask_simply_dome ', ' _static_fold ' Er ': ' mystatic ', ' extensions ': {}, ' Url_default_functions ': {}, ' url_build_error_handlers ': []}{}1--------------- -----{' subdomain_matching ': False, ' Error_handler_spec ': {}, ' _before_request_lock ': < Thread.lock object at 0x0000000002619d70 >, ' Before_request_funcs ': {}, ' Teardown_appcontext_funcs ': [], ' shell_context_processors ': [], ' after_request_ Funcs ': {}, ' CLI ': < Flask.cli.AppGroup object at 0x0000000002ef03c8 >, ' _blueprint_order ': [], ' Before_fi Rst_request_funcs ': [], ' view_functions ': {' static ': < bound method Flask.send_static_file of < Flask ' AS Imply_app ' >>}, ' Instance_path ': ' d:\\myself\\flask_simply_dome\\instance ', ' Teardown_request_funcs ': {}, ' Url_value_preprocessors ': {}, ' config ': ibid, ' _static_url_path ': None, ' template_context_processors ': { None: [< function _default_template_ctx_processor at 0x0000000002ed9ba8 >]}, ' Template_folder ': ' Templates ' , ' blueprints ': {}, ' Url_map ': Map ([< Rule '/static/<filename> ' (HEAD, OPTIONS, GET)-> Static >]) , ' name ': ' Asimply_app ', ' _got_first_request ': False, ' import_name ': ' __main__ ', ' root_path ': ' D:\\myself\\fla Sk_simply_dome ', ' _stAtic_folder ': ' Static ', ' extensions ': {}, ' Url_default_functions ': {}, ' url_build_error_handlers ': []}2---------- ----------{' subdomain_matching ': False, ' Error_handler_spec ': {}, ' _before_request_lock ': < Thread.lock objec T at 0x00000000026a3db0 > ' Before_request_funcs ': {}, ' Teardown_appcontext_funcs ': [], ' shell_context_proces Sors ': [], ' After_request_funcs ': {}, ' CLI ': < Flask.cli.AppGroup object at 0x0000000002ffb5f8 >, ' _bluepr Int_order ': [], ' Before_first_request_funcs ': [], ' view_functions ': {' static ': < bound method Flask.send_ Static_file of < Flask ' Asimply_app ' >>}, ' Instance_path ': ' D:\\myself\\flask_simply_dome\\instance ', ' Teardown_request_funcs ': {}, ' Url_value_preprocessors ': {}, ' config ': ibid., ' _static_url_path ': ', ' Template_co Ntext_processors ': {None: [< function _default_template_ctx_processor at 0x0000000002fdeba8 >]}, ' t Emplate_folder ': ' TemplaTEs ', ' blueprints ': {}, ' Url_map ': Map ([< Rule '/<filename> ' (HEAD, OPTIONS, GET)-> Static >]), ' Name ': ' Asimply_app ', ' _got_first_request ': False, ' import_name ': ' __main__ ', ' root_path ': ' D:\\myself\\flask_ Simply_dome ', ' _static_folder ': ' Static ', ' extensions ': {}, ' Url_default_functions ': {}, ' Url_build_error_hand Lers ': []}3----------------------"" ""
Conclusion
static_url_path主要用于改变url的path的,静态文件放在static下面,所以正常情况url是static/filename ,但是可以通过static_url_path来改变这个urlstatic_folder主要是用来改变url的目录的,默认是static,可以通过这个变量来改变静态文件目录。要是项目是前后端分离的话,那这里的配置也就没影响了。
The difference and connection between Static_folder and Static_url_path in flask