app.url_mapstores an object that maps and matches rules to endpoints. app.view_functionsdisplays endpoints for viewing features.
Call matchto match URL to endpoint and values. It will increase 404 if the route is not found, and 405 if the wrong method is specified. You will need the method as well as the corresponding 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:
return get_view_function(e.new_url, method)
except (MethodNotAllowed, NotFound):
return None
try:
return app.view_functions[match[0]], match[1]
except KeyError:
return None
, bind, , . . .
404 ( ) , , URL- , , 200.