Возврат URL-адреса в Flask к аргументам endpoint +

каким будет соответствующий способ разрешения URL-адреса в Flask для получения ссылки на конечную точку, а также словаря всех аргументов?

чтобы привести пример, учитывая этот маршрут, я хотел бы решить '/user/nick' to profile,{'username': 'nick'}:

@app.route('/user/<username>')
def profile(username): pass

из моих исследований до сих пор, все маршруты в колбе хранятся под app.url_map. Карта является экземпляром werkzeug.маршрутизирующий.Карта и у него есть метод match() это в принципе сделает то, что Я ищу. Однако этот метод является внутренним для класса.

3 ответов


это то, что я взломал для этой цели, глядя на url_for() и наоборот:

from flask.globals import _app_ctx_stack, _request_ctx_stack
from werkzeug.urls import url_parse

def route_from(url, method = None):
    appctx = _app_ctx_stack.top
    reqctx = _request_ctx_stack.top
    if appctx is None:
        raise RuntimeError('Attempted to match a URL without the '
                           'application context being pushed. This has to be '
                           'executed when application context is available.')

    if reqctx is not None:
        url_adapter = reqctx.url_adapter
    else:
        url_adapter = appctx.url_adapter
        if url_adapter is None:
            raise RuntimeError('Application was not able to create a URL '
                               'adapter for request independent URL matching. '
                               'You might be able to fix this by setting '
                               'the SERVER_NAME config variable.')
    parsed_url = url_parse(url)
    if parsed_url.netloc is not "" and parsed_url.netloc != url_adapter.server_name:
        raise NotFound()
    return url_adapter.match(parsed_url.path, method)

возвращаемым значением этого метода является кортеж, первый элемент которого является именем конечной точки, а второй-словарем с аргументами.

Я еще не протестировали, но он работал для меня во всех случаях.


Я знаю, что опаздываю с ответом, но я столкнулся с той же проблемой и нашел более простой способ ее получить:request.view_args. Например:

на мой взгляд:

@app.route('/user/<username>')
def profile(username): 
    return render_template("profile.html")

на profile.html: {{запрос.view_args}}

при посещении url http://localhost:4999/user/sam, Я: {'username': u'sam'}.

вы также можете получить имя функции, которая есть ваше мнение с request.endpoint.


я переписал реализацию Мигеля для поддержки поддоменов:

from flask import current_app, request
from six.moves.urllib.parse import urlsplit

def endpoint_for(url, method=None, return_rule=False, follow_redirects=True):
    """
    Given an absolute URL, retrieve the matching endpoint name (or rule).
    Requires a current request context to determine runtime environment.

    :param str method: HTTP method to use (defaults to GET)
    :param bool return_rule: Return the URL rule instead of the endpoint name
    :param bool follow_redirects: Follow redirects to final endpoint
    :return: Endpoint name or URL rule, or `None` if not found
    """
    parsed_url = urlsplit(url)
    if not parsed_url.netloc:
        # We require an absolute URL
        return

    # Take the current runtime environment...
    environ = dict(request.environ)
    # ...but replace the HTTP host with the URL's host...
    environ['HTTP_HOST'] = parsed_url.netloc
    # ...and the path with the URL's path (after discounting the app path, if not hosted at root).
    environ['PATH_INFO'] = parsed_url.path[len(environ['SCRIPT_NAME']):]
    # Create a new request with this environment...
    url_request = current_app.request_class(environ)
    # ...and a URL adapter with the new request.
    url_adapter = current_app.create_url_adapter(url_request)

    # Domain or subdomain must match.
    # TODO: To support apps that host multiple domains, we need to remove this
    # check, or offer a callback hook to check the domain.
    if parsed_url.netloc != url_adapter.server_name and not (
            parsed_url.netloc.endswith('.' + url_adapter.server_name)):
        return

    try:
        endpoint_or_rule, view_args = url_adapter.match(parsed_url.path, method, return_rule=return_rule)
        return endpoint_or_rule
    except RequestRedirect as r:
        # A redirect typically implies `/folder` -> `/folder/`
        # This will not be a redirect response from a view, since the view isn't being called
        if follow_redirects:
            return endpoint_for(r.new_url, method=method, return_rule=return_rule, follow_redirects=follow_redirects)
    except HTTPException as e:
        pass
    # If we got here, no endpoint was found.