在烧瓶蓝图中,我有:
frontend = Blueprint('frontend', __name__)
我的索引函数的路径是:
@frontend.route('/')
def index():
#code
这工作正常,但是,我试图向路由添加一个子域,如下所示:
@frontend.route('/', subdomain='<var>')
def index(var):
但这会破坏应用程序并且浏览器会输出(除其他外):
werkzeug.routing.BuildError
BuildError: ('frontend.index', {}, None)
frontend.index 在我的代码中的几个地方被调用url_for('前端.index')
当我包含子域时,如何使 url_for 正常工作?我在文件中唯一能找到并且我认为可能相关的是下面的内容http://flask.pocoo.org/docs/api/ http://flask.pocoo.org/docs/api/:
为了集成应用程序,Flask 有一个钩子来拦截 URL 构建
通过 Flask.build_error_handler 处理错误。 url_for 函数结果
当当前应用程序没有给定的 URL 时出现 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 None:
# External lookup did not have a URL.
# Re-raise the BuildError, in context of original traceback.
exc_type, exc_value, tb = sys.exc_info()
if exc_value is error:
raise exc_type, exc_value, tb
else:
raise error
# url_for will use this result, instead of raising BuildError.
return url
app.build_error_handler = external_url_handler
但是,我对 python(和编程)很陌生,无法理解我应该把这段代码放在哪里,或者当发生构建错误时如何调用该函数。
任何见解将不胜感激:)