获取匹配 url 的 Flask 视图函数
Get the Flask view function that matches a url
我有一些 url 路径,想检查它们是否指向我的 Flask 应用程序中的 url 规则。我如何使用 Flask 检查这个?
from flask import Flask, json, request, Response
app = Flask('simple_app')
@app.route('/foo/<bar_id>', methods=['GET'])
def foo_bar_id(bar_id):
if request.method == 'GET':
return Response(json.dumps({'foo': bar_id}), status=200)
@app.route('/bar', methods=['GET'])
def bar():
if request.method == 'GET':
return Response(json.dumps(['bar']), status=200)
test_route_a = '/foo/1' # return foo_bar_id function
test_route_b = '/bar' # return bar function
app.url_map
stores the object that maps and matches rules with endpoints. app.view_functions
将端点映射到视图函数。
调用 match
将 url 与端点和值匹配。如果找不到路由,它将引发 404,如果指定了错误的方法,则会引发 405。您需要方法以及 url 来匹配。
重定向被视为异常,您需要递归地捕获并测试它们以找到视图函数。
可以添加不映射到视图的规则,您需要在查找视图时捕获 KeyError
。
from werkzeug.routing import RequestRedirect, MethodNotAllowed, NotFound
def get_view_function(url, method='GET'):
"""Match a url and return the view and arguments
it will be called with, or None if there is no view.
"""
adapter = app.url_map.bind('localhost')
try:
match = adapter.match(url, method=method)
except RequestRedirect as e:
# recursively match redirects
return get_view_function(e.new_url, method)
except (MethodNotAllowed, NotFound):
# no match
return None
try:
# return the view function and arguments
return app.view_functions[match[0]], match[1]
except KeyError:
# no view is associated with the endpoint
return None
还有更多选项可以传递给 bind
以影响如何进行匹配,请参阅文档了解详细信息。
视图函数也可以引发 404(或其他)错误,因此这只能保证 url 将匹配视图,而不是视图 returns 200 响应。
除了@davidism 的回答(flask 的核心开发者)。
注意,如果你想发现当前url被flask app处理的view函数。您可以使用烧瓶的 Request
对象:
The request object used by default in Flask. Remembers the
matched endpoint and view arguments.
和 Flask.view_functtions
其中:
#: A dictionary mapping endpoint names to view functions.
#: To register a view function, use the :meth:route
decorator.
def get_view_function():
if request.url_rule:
return current_app.view_functions.get(request.url_rule.endpoint, None)
我有一些 url 路径,想检查它们是否指向我的 Flask 应用程序中的 url 规则。我如何使用 Flask 检查这个?
from flask import Flask, json, request, Response
app = Flask('simple_app')
@app.route('/foo/<bar_id>', methods=['GET'])
def foo_bar_id(bar_id):
if request.method == 'GET':
return Response(json.dumps({'foo': bar_id}), status=200)
@app.route('/bar', methods=['GET'])
def bar():
if request.method == 'GET':
return Response(json.dumps(['bar']), status=200)
test_route_a = '/foo/1' # return foo_bar_id function
test_route_b = '/bar' # return bar function
app.url_map
stores the object that maps and matches rules with endpoints. app.view_functions
将端点映射到视图函数。
调用 match
将 url 与端点和值匹配。如果找不到路由,它将引发 404,如果指定了错误的方法,则会引发 405。您需要方法以及 url 来匹配。
重定向被视为异常,您需要递归地捕获并测试它们以找到视图函数。
可以添加不映射到视图的规则,您需要在查找视图时捕获 KeyError
。
from werkzeug.routing import RequestRedirect, MethodNotAllowed, NotFound
def get_view_function(url, method='GET'):
"""Match a url and return the view and arguments
it will be called with, or None if there is no view.
"""
adapter = app.url_map.bind('localhost')
try:
match = adapter.match(url, method=method)
except RequestRedirect as e:
# recursively match redirects
return get_view_function(e.new_url, method)
except (MethodNotAllowed, NotFound):
# no match
return None
try:
# return the view function and arguments
return app.view_functions[match[0]], match[1]
except KeyError:
# no view is associated with the endpoint
return None
还有更多选项可以传递给 bind
以影响如何进行匹配,请参阅文档了解详细信息。
视图函数也可以引发 404(或其他)错误,因此这只能保证 url 将匹配视图,而不是视图 returns 200 响应。
除了@davidism 的回答(flask 的核心开发者)。
注意,如果你想发现当前url被flask app处理的view函数。您可以使用烧瓶的 Request
对象:
The request object used by default in Flask. Remembers the matched endpoint and view arguments.
和 Flask.view_functtions
其中:
#: A dictionary mapping endpoint names to view functions. #: To register a view function, use the :meth:
route
decorator.
def get_view_function():
if request.url_rule:
return current_app.view_functions.get(request.url_rule.endpoint, None)