使用Flask蓝图,如何解决如果指定一个子域的破坏url_for? [英] Using Flask Blueprints, how to fix url_for from breaking if a subdomain is specified?
问题描述
frontend = Blueprint('frontend',__name__)
和我的索引函数的路径是:
<$ p $ ($)
def index():
#code
这工作正常,但是,我想添加一个子域到路线,如下所示: 但是,这打破了应用程序和浏览器吐出(除其他外): frontend.index是在我的代码中的几个地方调用的,它在一个 url_for('frontend.index') 当我包含一个子域名时,我得到了url_for的工作?在我能找到的文件中,唯一能找到的,我认为可能是相关的,在 http:// flask。 为了集成应用程序,Flask有一个钩子来拦截URL生成 然而,我是新来的python(和编程),不能理解我将这个代码放在哪里,或者当builderror发生时我怎么得到这个函数调用。 任何洞察力将不胜感激:) 首先,要使用子域,您需要为SERVER_NAME 配置: 您有这样的观点: @ frontend.route
@frontend.route('/',subdomain ='< var>)
def index(var):
werkzeug.routing.BuildError
BuildError :('frontend.index',{},None)
$ b
通过Flask.build_error_handler错误。如果当前应用程序没有给定
端点和值的URL,则url_for函数会在BuildError中产生
。当它的时候,current_app调用它的
build_error_handler,如果它不是None,它可以返回一个字符串到
作为url_for的结果(而不是url_for的默认值来提高
的BuildError例外)或重新提出异常。例如:
def external_url_handler(error,endpoint,** values):
当url_for`无法构建URL时,查找外部URL
#这是hook了build_error_handler的一个例子。
#在这里,lookup_url是你构建的
#的一些实用函数,它在一些外部URL注册表中查找端点。
url = lookup_url(endpoint,** values)
如果url是None:
#外部查询没有URL。
#在原始追溯的上下文中重新引发BuildError。
exc_type,exc_value,tb = sys.exc_info()
如果exc_value是错误的话:
raise exc_type,exc_value,tb
else:
产生错误
#url_for将使用此结果,而不是引发BuildError。
return url
app.build_error_handler = external_url_handler
app.config [ 'SERVER_NAME'] ='example.net'
$ b $
frontend = Blueprint('frontend',__name__)
@frontend.route('/',subdomain ='< var> ')
def index(var):
return ...
为了重建这个视图的URL,Flask需要一个 var 的值。 url_for('frontend.index')
将失败,因为它没有足够的值。使用上面的SERVER_NAME, url_for('frontend.index',var ='foo')
将返回 http://foo.example.net/
。
Inside of a flask blueprint, i have:
frontend = Blueprint('frontend', __name__)
and the route to my index function is:
@frontend.route('/')
def index():
#code
This works fine but, I am trying to add a subdomain to the route, like so:
@frontend.route('/', subdomain='<var>')
def index(var):
But this breaks the app and the browser spits out (amongst other things):
werkzeug.routing.BuildError
BuildError: ('frontend.index', {}, None)
frontend.index is called out in my code in a few places in a url_for('frontend.index')
How can I get the url_for to work when I'm including a subdomain? The only thing in the documents I can find and I think might be relevant is this under http://flask.pocoo.org/docs/api/:
To integrate applications, Flask has a hook to intercept URL build errors through Flask.build_error_handler. The url_for function results in a BuildError when the current app does not have a URL for the given endpoint and values. When it does, the current_app calls its build_error_handler if it is not None, which can return a string to use as the result of url_for (instead of url_for‘s default to raise the BuildError exception) or re-raise the exception. An example:
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
However, I am new to python (and programming) and can not understand where I would put this code or how I would get that function to call when a builderror occurs.
Any insight would be greatly appreciated :)
First, to use subdomains you need to have a value for the SERVER_NAME configuration:
app.config['SERVER_NAME'] = 'example.net'
You have a view like this:
frontend = Blueprint('frontend', __name__)
@frontend.route('/', subdomain='<var>')
def index(var):
return ...
In order to reconstruct the URL to this view, Flask needs a value for var. url_for('frontend.index')
will fail since it does not have enough values. With the above SERVER_NAME, url_for('frontend.index', var='foo')
will return http://foo.example.net/
.
这篇关于使用Flask蓝图,如何解决如果指定一个子域的破坏url_for?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!