2016-05-19 57 views
1

我一整天都對燒瓶的子域感到困惑。如何重寫關於瓶的靜態url?

而且代碼就是這樣

from flask import Flask, Blueprint, url_for 

app = Flask(__name__) 
app.config['SERVER_NAME'] = 'localhost:5000' 
app.url_map.default_subdomain = 'forums' 

forums = Blueprint('forums', __name__) 


@forums.route('/') 
def forums_index(): 
    print(url_for('static', filename='index.css')) 
    return 'forums,hello' 

docs = Blueprint('docs', __name__) 


@docs.route('/') 
def docs_index(): 
    return 'docs,hello' 


app.register_blueprint(forums) 
app.register_blueprint(docs, subdomain='docs') 


@app.route('/hello') 
def index(): 
    return 'index,hello' 


if __name__ == '__main__': 
    app.run(debug=True) 
    print(app.url_map) 

的所有網址:

Map([<Rule 'forums|/hello' (HEAD, GET, OPTIONS) -> index>, 
<Rule 'forums|/' (HEAD, GET, OPTIONS) -> forums.forums_index>, 
<Rule 'docs|/' (HEAD, GET, OPTIONS) -> docs.docs_index>, 
<Rule '/static/<filename>' (HEAD, GET, OPTIONS) -> static>]) 

我訪問forums.localhost:5000,打印想這 http://localhost:5000/static/index.css

我想我添加default_subdomain ,打印應該是
http://forums.localhost:5000/static/index.css

現在,我用url_for('static',filename='index.css')在templates.But我不能Pruduction環境得到css文件。

我該如何重寫靜態url? 我看看燒瓶的來源。並添加

app.add_url_rule(app.static_url_path + '/<path:filename>', 
       endpoint='static', 
       view_func=app.send_static_file, 
       subdomain='forums') 

不幸的是,它並沒有覆蓋原有的靜態url。 那麼,如何重寫靜態URL約燒瓶感謝

回答

0

我發現這裏https://github.com/pallets/flask/issues/1559

一個答案只需添加

app.url_map._rules.clear() 
app.url_map._rules_by_endpoint.clear() 

app = Flask(__name__) 然後所有網址:

Map([<Rule 'forums|/hello' (HEAD, GET, OPTIONS) -> index>, 
<Rule 'forums|/' (HEAD, GET, OPTIONS) -> forums.forums_index>, 
<Rule 'docs|/' (HEAD, GET, OPTIONS) -> docs.docs_index>, 
<Rule 'forums|/static/<filename>' (HEAD, GET, OPTIONS) -> static>])