相关疑难解决方法(0)

使用Flask蓝图,如果指定了子域,如何修复url_for?

在烧瓶蓝图中,我有:

frontend = Blueprint('frontend', __name__)
Run Code Online (Sandbox Code Playgroud)

和我的索引功能的路线是:

@frontend.route('/')
def index():
  #code
Run Code Online (Sandbox Code Playgroud)

这工作正常但是,我试图在路由中添加子域,如下所示:

@frontend.route('/', subdomain='<var>')
def index(var):
Run Code Online (Sandbox Code Playgroud)

但这打破了应用程序,浏览器吐出(除其他外):

werkzeug.routing.BuildError
BuildError: ('frontend.index', {}, None)
Run Code Online (Sandbox Code Playgroud)

我的代码在url_for('frontend.index')中的几个地方调用frontend.index

当我包含子域时,如何让url_for工作?在http://flask.pocoo.org/docs/api/下,我能找到的文件中唯一可能与之相关的内容是:

为了集成应用程序,Flask有一个钩子来拦截通过Flask.build_error_handler拦截URL构建错误.当前应用程序没有给定端点和值的URL时,url_for函数会导致BuildError.如果是这样,current_app会调用它的build_error_handler,如果它不是None,它可以返回一个字符串,用作url_for的结果(而不是url_for的默认值引发BuildError异常)或重新引发异常.一个例子:

def external_url_handler(error, endpoint, **values):
    "Looks up an external URL when `url_for` cannot build a URL."
    # This is an example of hooking the build_error_handler.
    # Here, lookup_url is some utility function you've built
    # which looks up the endpoint in some external URL registry.
    url = lookup_url(endpoint, **values)
    if url is …
Run Code Online (Sandbox Code Playgroud)

python routes flask

8
推荐指数
2
解决办法
1万
查看次数

标签 统计

flask ×1

python ×1

routes ×1