Home / Function/ url_for() — flask Function Reference

url_for() — flask Function Reference

Architecture documentation for the url_for() function in app.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  d79553e1_9435_9b37_b132_99c938d6f250["url_for()"]
  9f8cc56e_d5c4_e7a8_438a_3124c0dd5de5["Flask"]
  d79553e1_9435_9b37_b132_99c938d6f250 -->|defined in| 9f8cc56e_d5c4_e7a8_438a_3124c0dd5de5
  50282238_729d_4729_21d4_c290921ffa61["create_url_adapter()"]
  d79553e1_9435_9b37_b132_99c938d6f250 -->|calls| 50282238_729d_4729_21d4_c290921ffa61
  7a5f6a6e_f5af_ab6f_cfee_01851e53dff5["url_for()"]
  d79553e1_9435_9b37_b132_99c938d6f250 -->|calls| 7a5f6a6e_f5af_ab6f_cfee_01851e53dff5
  c1644887_6834_cf5b_9551_6a1aa07a77d7["get()"]
  d79553e1_9435_9b37_b132_99c938d6f250 -->|calls| c1644887_6834_cf5b_9551_6a1aa07a77d7
  style d79553e1_9435_9b37_b132_99c938d6f250 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/flask/app.py lines 1101–1221

    def url_for(
        self,
        /,
        endpoint: str,
        *,
        _anchor: str | None = None,
        _method: str | None = None,
        _scheme: str | None = None,
        _external: bool | None = None,
        **values: t.Any,
    ) -> str:
        """Generate a URL to the given endpoint with the given values.

        This is called by :func:`flask.url_for`, and can be called
        directly as well.

        An *endpoint* is the name of a URL rule, usually added with
        :meth:`@app.route() <route>`, and usually the same name as the
        view function. A route defined in a :class:`~flask.Blueprint`
        will prepend the blueprint's name separated by a ``.`` to the
        endpoint.

        In some cases, such as email messages, you want URLs to include
        the scheme and domain, like ``https://example.com/hello``. When
        not in an active request, URLs will be external by default, but
        this requires setting :data:`SERVER_NAME` so Flask knows what
        domain to use. :data:`APPLICATION_ROOT` and
        :data:`PREFERRED_URL_SCHEME` should also be configured as
        needed. This config is only used when not in an active request.

        Functions can be decorated with :meth:`url_defaults` to modify
        keyword arguments before the URL is built.

        If building fails for some reason, such as an unknown endpoint
        or incorrect values, the app's :meth:`handle_url_build_error`
        method is called. If that returns a string, that is returned,
        otherwise a :exc:`~werkzeug.routing.BuildError` is raised.

        :param endpoint: The endpoint name associated with the URL to
            generate. If this starts with a ``.``, the current blueprint
            name (if any) will be used.
        :param _anchor: If given, append this as ``#anchor`` to the URL.
        :param _method: If given, generate the URL associated with this
            method for the endpoint.
        :param _scheme: If given, the URL will have this scheme if it
            is external.
        :param _external: If given, prefer the URL to be internal
            (False) or require it to be external (True). External URLs
            include the scheme and domain. When not in an active
            request, URLs are external by default.
        :param values: Values to use for the variable parts of the URL
            rule. Unknown keys are appended as query string arguments,
            like ``?a=b&c=d``.

        .. versionadded:: 2.2
            Moved from ``flask.url_for``, which calls this method.
        """
        if (ctx := _cv_app.get(None)) is not None and ctx.has_request:
            url_adapter = ctx.url_adapter
            blueprint_name = ctx.request.blueprint

            # If the endpoint starts with "." and the request matches a
            # blueprint, the endpoint is relative to the blueprint.
            if endpoint[:1] == ".":
                if blueprint_name is not None:
                    endpoint = f"{blueprint_name}{endpoint}"
                else:
                    endpoint = endpoint[1:]

            # When in a request, generate a URL without scheme and
            # domain by default, unless a scheme is given.
            if _external is None:
                _external = _scheme is not None
        else:
            # If called by helpers.url_for, an app context is active,
            # use its url_adapter. Otherwise, app.url_for was called
            # directly, build an adapter.
            if ctx is not None:
                url_adapter = ctx.url_adapter
            else:
                url_adapter = self.create_url_adapter(None)

Subdomains

Defined In

Frequently Asked Questions

What does url_for() do?
url_for() is a function in the flask codebase, defined in src/flask/app.py.
Where is url_for() defined?
url_for() is defined in src/flask/app.py at line 1101.
What does url_for() call?
url_for() calls 3 function(s): create_url_adapter, get, url_for.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free