如何重写关于烧瓶的静态网址?

时间:2016-05-19 13:58:18

标签: python flask jinja2

我一整天都对瓶子的子域感到困惑。

代码就像这样

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')。但我无法在Pruduction Environment中获取css文件。

如何重写静态网址? 我看看烧瓶的来源。并添加

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

不幸的是,它并没有覆盖原始的静态网址。 那么,如何重写关于烧瓶的静态网址?谢谢

1 个答案:

答案 0 :(得分: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>])