url_for Builderror在烧瓶延伸带可插拔的观点(url_for Builderror

2019-09-29 11:53发布

我建立了瓶的简单扩展,并具有不能够在扩展中构建的URL中url_for功能的问题。

有人可以帮我找出我在这里失踪?

我简化了代码来演示这个问题(所有url_for调用养WERKZEUG BuildError除外):

import flask
import flask.views

import logging
logging.basicConfig(level=logging.DEBUG)

class MyFlaskExt(object):
    def __init__(self, app=None):
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        self.blueprint = flask.Blueprint('myext', __name__, static_folder='static', template_folder='templates')
        self.blueprint.add_url_rule('/', view_func=RootView.as_view('root'), endpoint='root')
        self.blueprint.add_url_rule('/var/<somevar>', view_func=VarView.as_view('var'), endpoint='var')
        self.blueprint.add_url_rule('/novar', view_func=NoVarView.as_view('novar'), endpoint='novar')
        app.register_blueprint(self.blueprint)


class RootView(flask.views.View):
    def dispatch_request(self):
        logging.debug(flask.url_for('novar'))
        logging.debug(flask.url_for('novar', _external=True))
        return flask.redirect(flask.url_for('var', somevar='test'))

class VarView(flask.views.View):
    def dispatch_request(self, somevar):
        return "SUCCESS! ({})".format(somevar)

class NoVarView(flask.views.View):
    def dispatch_request(self):
        return "SUCCESS!"

if __name__ == '__main__':
    app = flask.Flask(__name__)
    app.debug = True
    my_ext = MyFlaskExt()
    my_ext.init_app(app)
    logging.debug(app.url_map)
    app.run()

Answer 1:

Blueprint端点的名义注册的Blueprint 。 如果我没有记错,你要么需要预先设置你的url_for用电话“” (如果他们都将在同一蓝图下运行),或使用全名:

def dispatch_request(self):
    logging.debug(flask.url_for('.novar'))
    logging.debug(flask.url_for('.novar', _external=True))
    return flask.redirect(flask.url_for('.var', somevar='test'))


文章来源: url_for Builderror in flask extension with pluggable views