Method: flask.app.Flask.url_for
Calls: 42, Exceptions: 7, Paths: 8Back
Path 1: 23 calls (0.55)
'static' (3) 'about' (3) 'index' (3) 'myview' (3) 'view' (2) 'hello' (1) 'something_else' (1) 'foo.view' (1) 'foo' (1) 'bar' (1)
None (23)
None (20) 'GET' (2) 'POST' (1)
None (22) 'https' (1)
None (23)
{} (13) {'filename': 'index.html'} (3) {'company_id': 'xxx'} (2) {'name': 'test x'} (1) {'lang_code': 'en'} (1) {'filename': 'test.txt'} (1) dict (1) ...
'/foo/index.html' (2) '/de/about' (2) '/hello/test%20x' (1) '/static/index.html' (1) '/foo' (1) '/en/about' (1) '/login' (1) '/foo/' (1) '/bar/' (1) '...
1def url_for(
2 self,
3 endpoint: str,
4 *,
5 _anchor: str | None = None,
6 _method: str | None = None,
7 _scheme: str | None = None,
8 _external: bool | None = None,
9 **values: t.Any,
10 ) -> str:
11 """Generate a URL to the given endpoint with the given values.
12
13 This is called by :func:`flask.url_for`, and can be called
14 directly as well.
15
16 An *endpoint* is the name of a URL rule, usually added with
17 :meth:`@app.route() <route>`, and usually the same name as the
18 view function. A route defined in a :class:`~flask.Blueprint`
19 will prepend the blueprint's name separated by a ``.`` to the
20 endpoint.
21
22 In some cases, such as email messages, you want URLs to include
23 the scheme and domain, like ``https://example.com/hello``. When
24 not in an active request, URLs will be external by default, but
25 this requires setting :data:`SERVER_NAME` so Flask knows what
26 domain to use. :data:`APPLICATION_ROOT` and
27 :data:`PREFERRED_URL_SCHEME` should also be configured as
28 needed. This config is only used when not in an active request.
29
30 Functions can be decorated with :meth:`url_defaults` to modify
31 keyword arguments before the URL is built.
32
33 If building fails for some reason, such as an unknown endpoint
34 or incorrect values, the app's :meth:`handle_url_build_error`
35 method is called. If that returns a string, that is returned,
36 otherwise a :exc:`~werkzeug.routing.BuildError` is raised.
37
38 :param endpoint: The endpoint name associated with the URL to
39 generate. If this starts with a ``.``, the current blueprint
40 name (if any) will be used.
41 :param _anchor: If given, append this as ``#anchor`` to the URL.
42 :param _method: If given, generate the URL associated with this
43 method for the endpoint.
44 :param _scheme: If given, the URL will have this scheme if it
45 is external.
46 :param _external: If given, prefer the URL to be internal
47 (False) or require it to be external (True). External URLs
48 include the scheme and domain. When not in an active
49 request, URLs are external by default.
50 :param values: Values to use for the variable parts of the URL
51 rule. Unknown keys are appended as query string arguments,
52 like ``?a=b&c=d``.
53
54 .. versionadded:: 2.2
55 Moved from ``flask.url_for``, which calls this method.
56 """
57 req_ctx = _cv_request.get(None)
58
59 if req_ctx is not None:
60 url_adapter = req_ctx.url_adapter
61 blueprint_name = req_ctx.request.blueprint
62
63 # If the endpoint starts with "." and the request matches a
64 # blueprint, the endpoint is relative to the blueprint.
65 if endpoint[:1] == ".":
66 if blueprint_name is not None:
67 endpoint = f"{blueprint_name}{endpoint}"
68 else:
69 endpoint = endpoint[1:]
70
71 # When in a request, generate a URL without scheme and
72 # domain by default, unless a scheme is given.
73 if _external is None:
74 _external = _scheme is not None
75 else:
76 app_ctx = _cv_app.get(None)
77
78 # If called by helpers.url_for, an app context is active,
79 # use its url_adapter. Otherwise, app.url_for was called
80 # directly, build an adapter.
81 if app_ctx is not None:
82 url_adapter = app_ctx.url_adapter
83 else:
84 url_adapter = self.create_url_adapter(None)
85
86 if url_adapter is None:
87 raise RuntimeError(
88 "Unable to build URLs outside an active request"
89 " without 'SERVER_NAME' configured. Also configure"
90 " 'APPLICATION_ROOT' and 'PREFERRED_URL_SCHEME' as"
91 " needed."
92 )
93
94 # When outside a request, generate a URL with scheme and
95 # domain by default.
96 if _external is None:
97 _external = True
98
99 # It is an error to set _scheme when _external=False, in order
100 # to avoid accidental insecure URLs.
101 if _scheme is not None and not _external:
102 raise ValueError("When specifying '_scheme', '_external' must be True.")
103
104 self.inject_url_defaults(endpoint, values)
105
106 try:
107 rv = url_adapter.build( # type: ignore[union-attr]
108 endpoint,
109 values,
110 method=_method,
111 url_scheme=_scheme,
112 force_external=_external,
113 )
114 except BuildError as error:
115 values.update(
116 _anchor=_anchor, _method=_method, _scheme=_scheme, _external=_external
117 )
118 return self.handle_url_build_error(error, endpoint, values)
119
120 if _anchor is not None:
121 _anchor = _url_quote(_anchor, safe="%!#$&'()*+,/:;=?@")
122 rv = f"{rv}#{_anchor}"
123
124 return rv
Path 2: 8 calls (0.19)
'index' (5) 'hello' (1) 'static' (1) 'sub' (1)
None (8)
None (8)
None (6) 'https' (2)
True (8)
{} (6) {'name': 'test x'} (1) {'filename': 'index.html'} (1)
'https://localhost/' (2) 'http://localhost/' (2) 'http://localhost/hello/test%20x' (1) 'http://example.com/static/index.html' (1) 'http://localhost.lo...
1def url_for(
2 self,
3 endpoint: str,
4 *,
5 _anchor: str | None = None,
6 _method: str | None = None,
7 _scheme: str | None = None,
8 _external: bool | None = None,
9 **values: t.Any,
10 ) -> str:
11 """Generate a URL to the given endpoint with the given values.
12
13 This is called by :func:`flask.url_for`, and can be called
14 directly as well.
15
16 An *endpoint* is the name of a URL rule, usually added with
17 :meth:`@app.route() <route>`, and usually the same name as the
18 view function. A route defined in a :class:`~flask.Blueprint`
19 will prepend the blueprint's name separated by a ``.`` to the
20 endpoint.
21
22 In some cases, such as email messages, you want URLs to include
23 the scheme and domain, like ``https://example.com/hello``. When
24 not in an active request, URLs will be external by default, but
25 this requires setting :data:`SERVER_NAME` so Flask knows what
26 domain to use. :data:`APPLICATION_ROOT` and
27 :data:`PREFERRED_URL_SCHEME` should also be configured as
28 needed. This config is only used when not in an active request.
29
30 Functions can be decorated with :meth:`url_defaults` to modify
31 keyword arguments before the URL is built.
32
33 If building fails for some reason, such as an unknown endpoint
34 or incorrect values, the app's :meth:`handle_url_build_error`
35 method is called. If that returns a string, that is returned,
36 otherwise a :exc:`~werkzeug.routing.BuildError` is raised.
37
38 :param endpoint: The endpoint name associated with the URL to
39 generate. If this starts with a ``.``, the current blueprint
40 name (if any) will be used.
41 :param _anchor: If given, append this as ``#anchor`` to the URL.
42 :param _method: If given, generate the URL associated with this
43 method for the endpoint.
44 :param _scheme: If given, the URL will have this scheme if it
45 is external.
46 :param _external: If given, prefer the URL to be internal
47 (False) or require it to be external (True). External URLs
48 include the scheme and domain. When not in an active
49 request, URLs are external by default.
50 :param values: Values to use for the variable parts of the URL
51 rule. Unknown keys are appended as query string arguments,
52 like ``?a=b&c=d``.
53
54 .. versionadded:: 2.2
55 Moved from ``flask.url_for``, which calls this method.
56 """
57 req_ctx = _cv_request.get(None)
58
59 if req_ctx is not None:
60 url_adapter = req_ctx.url_adapter
61 blueprint_name = req_ctx.request.blueprint
62
63 # If the endpoint starts with "." and the request matches a
64 # blueprint, the endpoint is relative to the blueprint.
65 if endpoint[:1] == ".":
66 if blueprint_name is not None:
67 endpoint = f"{blueprint_name}{endpoint}"
68 else:
69 endpoint = endpoint[1:]
70
71 # When in a request, generate a URL without scheme and
72 # domain by default, unless a scheme is given.
73 if _external is None:
74 _external = _scheme is not None
75 else:
76 app_ctx = _cv_app.get(None)
77
78 # If called by helpers.url_for, an app context is active,
79 # use its url_adapter. Otherwise, app.url_for was called
80 # directly, build an adapter.
81 if app_ctx is not None:
82 url_adapter = app_ctx.url_adapter
83 else:
84 url_adapter = self.create_url_adapter(None)
85
86 if url_adapter is None:
87 raise RuntimeError(
88 "Unable to build URLs outside an active request"
89 " without 'SERVER_NAME' configured. Also configure"
90 " 'APPLICATION_ROOT' and 'PREFERRED_URL_SCHEME' as"
91 " needed."
92 )
93
94 # When outside a request, generate a URL with scheme and
95 # domain by default.
96 if _external is None:
97 _external = True
98
99 # It is an error to set _scheme when _external=False, in order
100 # to avoid accidental insecure URLs.
101 if _scheme is not None and not _external:
102 raise ValueError("When specifying '_scheme', '_external' must be True.")
103
104 self.inject_url_defaults(endpoint, values)
105
106 try:
107 rv = url_adapter.build( # type: ignore[union-attr]
108 endpoint,
109 values,
110 method=_method,
111 url_scheme=_scheme,
112 force_external=_external,
113 )
114 except BuildError as error:
115 values.update(
116 _anchor=_anchor, _method=_method, _scheme=_scheme, _external=_external
117 )
118 return self.handle_url_build_error(error, endpoint, values)
119
120 if _anchor is not None:
121 _anchor = _url_quote(_anchor, safe="%!#$&'()*+,/:;=?@")
122 rv = f"{rv}#{_anchor}"
123
124 return rv
Path 3: 5 calls (0.12)
'spam' (3) 'not.existing' (1) '/' (1)
None (5)
None (5)
None (5)
None (5)
{} (5)
None (3) '/test_handler/' (1) 'handled' (1)
BuildError (5)
1def url_for(
2 self,
3 endpoint: str,
4 *,
5 _anchor: str | None = None,
6 _method: str | None = None,
7 _scheme: str | None = None,
8 _external: bool | None = None,
9 **values: t.Any,
10 ) -> str:
11 """Generate a URL to the given endpoint with the given values.
12
13 This is called by :func:`flask.url_for`, and can be called
14 directly as well.
15
16 An *endpoint* is the name of a URL rule, usually added with
17 :meth:`@app.route() <route>`, and usually the same name as the
18 view function. A route defined in a :class:`~flask.Blueprint`
19 will prepend the blueprint's name separated by a ``.`` to the
20 endpoint.
21
22 In some cases, such as email messages, you want URLs to include
23 the scheme and domain, like ``https://example.com/hello``. When
24 not in an active request, URLs will be external by default, but
25 this requires setting :data:`SERVER_NAME` so Flask knows what
26 domain to use. :data:`APPLICATION_ROOT` and
27 :data:`PREFERRED_URL_SCHEME` should also be configured as
28 needed. This config is only used when not in an active request.
29
30 Functions can be decorated with :meth:`url_defaults` to modify
31 keyword arguments before the URL is built.
32
33 If building fails for some reason, such as an unknown endpoint
34 or incorrect values, the app's :meth:`handle_url_build_error`
35 method is called. If that returns a string, that is returned,
36 otherwise a :exc:`~werkzeug.routing.BuildError` is raised.
37
38 :param endpoint: The endpoint name associated with the URL to
39 generate. If this starts with a ``.``, the current blueprint
40 name (if any) will be used.
41 :param _anchor: If given, append this as ``#anchor`` to the URL.
42 :param _method: If given, generate the URL associated with this
43 method for the endpoint.
44 :param _scheme: If given, the URL will have this scheme if it
45 is external.
46 :param _external: If given, prefer the URL to be internal
47 (False) or require it to be external (True). External URLs
48 include the scheme and domain. When not in an active
49 request, URLs are external by default.
50 :param values: Values to use for the variable parts of the URL
51 rule. Unknown keys are appended as query string arguments,
52 like ``?a=b&c=d``.
53
54 .. versionadded:: 2.2
55 Moved from ``flask.url_for``, which calls this method.
56 """
57 req_ctx = _cv_request.get(None)
58
59 if req_ctx is not None:
60 url_adapter = req_ctx.url_adapter
61 blueprint_name = req_ctx.request.blueprint
62
63 # If the endpoint starts with "." and the request matches a
64 # blueprint, the endpoint is relative to the blueprint.
65 if endpoint[:1] == ".":
66 if blueprint_name is not None:
67 endpoint = f"{blueprint_name}{endpoint}"
68 else:
69 endpoint = endpoint[1:]
70
71 # When in a request, generate a URL without scheme and
72 # domain by default, unless a scheme is given.
73 if _external is None:
74 _external = _scheme is not None
75 else:
76 app_ctx = _cv_app.get(None)
77
78 # If called by helpers.url_for, an app context is active,
79 # use its url_adapter. Otherwise, app.url_for was called
80 # directly, build an adapter.
81 if app_ctx is not None:
82 url_adapter = app_ctx.url_adapter
83 else:
84 url_adapter = self.create_url_adapter(None)
85
86 if url_adapter is None:
87 raise RuntimeError(
88 "Unable to build URLs outside an active request"
89 " without 'SERVER_NAME' configured. Also configure"
90 " 'APPLICATION_ROOT' and 'PREFERRED_URL_SCHEME' as"
91 " needed."
92 )
93
94 # When outside a request, generate a URL with scheme and
95 # domain by default.
96 if _external is None:
97 _external = True
98
99 # It is an error to set _scheme when _external=False, in order
100 # to avoid accidental insecure URLs.
101 if _scheme is not None and not _external:
102 raise ValueError("When specifying '_scheme', '_external' must be True.")
103
104 self.inject_url_defaults(endpoint, values)
105
106 try:
107 rv = url_adapter.build( # type: ignore[union-attr]
108 endpoint,
109 values,
110 method=_method,
111 url_scheme=_scheme,
112 force_external=_external,
113 )
114 except BuildError as error:
115 values.update(
116 _anchor=_anchor, _method=_method, _scheme=_scheme, _external=_external
117 )
118 return self.handle_url_build_error(error, endpoint, values)
119
120 if _anchor is not None:
121 _anchor = _url_quote(_anchor, safe="%!#$&'()*+,/:;=?@")
122 rv = f"{rv}#{_anchor}"
123
124 return rv
Path 4: 2 calls (0.05)
'.about' (1) '.index' (1)
None (2)
None (2)
None (2)
None (2)
{} (2)
'/de/about' (1) '/de/' (1)
1def url_for(
2 self,
3 endpoint: str,
4 *,
5 _anchor: str | None = None,
6 _method: str | None = None,
7 _scheme: str | None = None,
8 _external: bool | None = None,
9 **values: t.Any,
10 ) -> str:
11 """Generate a URL to the given endpoint with the given values.
12
13 This is called by :func:`flask.url_for`, and can be called
14 directly as well.
15
16 An *endpoint* is the name of a URL rule, usually added with
17 :meth:`@app.route() <route>`, and usually the same name as the
18 view function. A route defined in a :class:`~flask.Blueprint`
19 will prepend the blueprint's name separated by a ``.`` to the
20 endpoint.
21
22 In some cases, such as email messages, you want URLs to include
23 the scheme and domain, like ``https://example.com/hello``. When
24 not in an active request, URLs will be external by default, but
25 this requires setting :data:`SERVER_NAME` so Flask knows what
26 domain to use. :data:`APPLICATION_ROOT` and
27 :data:`PREFERRED_URL_SCHEME` should also be configured as
28 needed. This config is only used when not in an active request.
29
30 Functions can be decorated with :meth:`url_defaults` to modify
31 keyword arguments before the URL is built.
32
33 If building fails for some reason, such as an unknown endpoint
34 or incorrect values, the app's :meth:`handle_url_build_error`
35 method is called. If that returns a string, that is returned,
36 otherwise a :exc:`~werkzeug.routing.BuildError` is raised.
37
38 :param endpoint: The endpoint name associated with the URL to
39 generate. If this starts with a ``.``, the current blueprint
40 name (if any) will be used.
41 :param _anchor: If given, append this as ``#anchor`` to the URL.
42 :param _method: If given, generate the URL associated with this
43 method for the endpoint.
44 :param _scheme: If given, the URL will have this scheme if it
45 is external.
46 :param _external: If given, prefer the URL to be internal
47 (False) or require it to be external (True). External URLs
48 include the scheme and domain. When not in an active
49 request, URLs are external by default.
50 :param values: Values to use for the variable parts of the URL
51 rule. Unknown keys are appended as query string arguments,
52 like ``?a=b&c=d``.
53
54 .. versionadded:: 2.2
55 Moved from ``flask.url_for``, which calls this method.
56 """
57 req_ctx = _cv_request.get(None)
58
59 if req_ctx is not None:
60 url_adapter = req_ctx.url_adapter
61 blueprint_name = req_ctx.request.blueprint
62
63 # If the endpoint starts with "." and the request matches a
64 # blueprint, the endpoint is relative to the blueprint.
65 if endpoint[:1] == ".":
66 if blueprint_name is not None:
67 endpoint = f"{blueprint_name}{endpoint}"
68 else:
69 endpoint = endpoint[1:]
70
71 # When in a request, generate a URL without scheme and
72 # domain by default, unless a scheme is given.
73 if _external is None:
74 _external = _scheme is not None
75 else:
76 app_ctx = _cv_app.get(None)
77
78 # If called by helpers.url_for, an app context is active,
79 # use its url_adapter. Otherwise, app.url_for was called
80 # directly, build an adapter.
81 if app_ctx is not None:
82 url_adapter = app_ctx.url_adapter
83 else:
84 url_adapter = self.create_url_adapter(None)
85
86 if url_adapter is None:
87 raise RuntimeError(
88 "Unable to build URLs outside an active request"
89 " without 'SERVER_NAME' configured. Also configure"
90 " 'APPLICATION_ROOT' and 'PREFERRED_URL_SCHEME' as"
91 " needed."
92 )
93
94 # When outside a request, generate a URL with scheme and
95 # domain by default.
96 if _external is None:
97 _external = True
98
99 # It is an error to set _scheme when _external=False, in order
100 # to avoid accidental insecure URLs.
101 if _scheme is not None and not _external:
102 raise ValueError("When specifying '_scheme', '_external' must be True.")
103
104 self.inject_url_defaults(endpoint, values)
105
106 try:
107 rv = url_adapter.build( # type: ignore[union-attr]
108 endpoint,
109 values,
110 method=_method,
111 url_scheme=_scheme,
112 force_external=_external,
113 )
114 except BuildError as error:
115 values.update(
116 _anchor=_anchor, _method=_method, _scheme=_scheme, _external=_external
117 )
118 return self.handle_url_build_error(error, endpoint, values)
119
120 if _anchor is not None:
121 _anchor = _url_quote(_anchor, safe="%!#$&'()*+,/:;=?@")
122 rv = f"{rv}#{_anchor}"
123
124 return rv
Path 5: 1 calls (0.02)
'index' (1)
None (1)
None (1)
None (1)
None (1)
{} (1)
'https://localhost/' (1)
1def url_for(
2 self,
3 endpoint: str,
4 *,
5 _anchor: str | None = None,
6 _method: str | None = None,
7 _scheme: str | None = None,
8 _external: bool | None = None,
9 **values: t.Any,
10 ) -> str:
11 """Generate a URL to the given endpoint with the given values.
12
13 This is called by :func:`flask.url_for`, and can be called
14 directly as well.
15
16 An *endpoint* is the name of a URL rule, usually added with
17 :meth:`@app.route() <route>`, and usually the same name as the
18 view function. A route defined in a :class:`~flask.Blueprint`
19 will prepend the blueprint's name separated by a ``.`` to the
20 endpoint.
21
22 In some cases, such as email messages, you want URLs to include
23 the scheme and domain, like ``https://example.com/hello``. When
24 not in an active request, URLs will be external by default, but
25 this requires setting :data:`SERVER_NAME` so Flask knows what
26 domain to use. :data:`APPLICATION_ROOT` and
27 :data:`PREFERRED_URL_SCHEME` should also be configured as
28 needed. This config is only used when not in an active request.
29
30 Functions can be decorated with :meth:`url_defaults` to modify
31 keyword arguments before the URL is built.
32
33 If building fails for some reason, such as an unknown endpoint
34 or incorrect values, the app's :meth:`handle_url_build_error`
35 method is called. If that returns a string, that is returned,
36 otherwise a :exc:`~werkzeug.routing.BuildError` is raised.
37
38 :param endpoint: The endpoint name associated with the URL to
39 generate. If this starts with a ``.``, the current blueprint
40 name (if any) will be used.
41 :param _anchor: If given, append this as ``#anchor`` to the URL.
42 :param _method: If given, generate the URL associated with this
43 method for the endpoint.
44 :param _scheme: If given, the URL will have this scheme if it
45 is external.
46 :param _external: If given, prefer the URL to be internal
47 (False) or require it to be external (True). External URLs
48 include the scheme and domain. When not in an active
49 request, URLs are external by default.
50 :param values: Values to use for the variable parts of the URL
51 rule. Unknown keys are appended as query string arguments,
52 like ``?a=b&c=d``.
53
54 .. versionadded:: 2.2
55 Moved from ``flask.url_for``, which calls this method.
56 """
57 req_ctx = _cv_request.get(None)
58
59 if req_ctx is not None:
60 url_adapter = req_ctx.url_adapter
61 blueprint_name = req_ctx.request.blueprint
62
63 # If the endpoint starts with "." and the request matches a
64 # blueprint, the endpoint is relative to the blueprint.
65 if endpoint[:1] == ".":
66 if blueprint_name is not None:
67 endpoint = f"{blueprint_name}{endpoint}"
68 else:
69 endpoint = endpoint[1:]
70
71 # When in a request, generate a URL without scheme and
72 # domain by default, unless a scheme is given.
73 if _external is None:
74 _external = _scheme is not None
75 else:
76 app_ctx = _cv_app.get(None)
77
78 # If called by helpers.url_for, an app context is active,
79 # use its url_adapter. Otherwise, app.url_for was called
80 # directly, build an adapter.
81 if app_ctx is not None:
82 url_adapter = app_ctx.url_adapter
83 else:
84 url_adapter = self.create_url_adapter(None)
85
86 if url_adapter is None:
87 raise RuntimeError(
88 "Unable to build URLs outside an active request"
89 " without 'SERVER_NAME' configured. Also configure"
90 " 'APPLICATION_ROOT' and 'PREFERRED_URL_SCHEME' as"
91 " needed."
92 )
93
94 # When outside a request, generate a URL with scheme and
95 # domain by default.
96 if _external is None:
97 _external = True
98
99 # It is an error to set _scheme when _external=False, in order
100 # to avoid accidental insecure URLs.
101 if _scheme is not None and not _external:
102 raise ValueError("When specifying '_scheme', '_external' must be True.")
103
104 self.inject_url_defaults(endpoint, values)
105
106 try:
107 rv = url_adapter.build( # type: ignore[union-attr]
108 endpoint,
109 values,
110 method=_method,
111 url_scheme=_scheme,
112 force_external=_external,
113 )
114 except BuildError as error:
115 values.update(
116 _anchor=_anchor, _method=_method, _scheme=_scheme, _external=_external
117 )
118 return self.handle_url_build_error(error, endpoint, values)
119
120 if _anchor is not None:
121 _anchor = _url_quote(_anchor, safe="%!#$&'()*+,/:;=?@")
122 rv = f"{rv}#{_anchor}"
123
124 return rv
Path 6: 1 calls (0.02)
'index' (1)
None (1)
None (1)
None (1)
None (1)
{} (1)
RuntimeError (1)
1def url_for(
2 self,
3 endpoint: str,
4 *,
5 _anchor: str | None = None,
6 _method: str | None = None,
7 _scheme: str | None = None,
8 _external: bool | None = None,
9 **values: t.Any,
10 ) -> str:
11 """Generate a URL to the given endpoint with the given values.
12
13 This is called by :func:`flask.url_for`, and can be called
14 directly as well.
15
16 An *endpoint* is the name of a URL rule, usually added with
17 :meth:`@app.route() <route>`, and usually the same name as the
18 view function. A route defined in a :class:`~flask.Blueprint`
19 will prepend the blueprint's name separated by a ``.`` to the
20 endpoint.
21
22 In some cases, such as email messages, you want URLs to include
23 the scheme and domain, like ``https://example.com/hello``. When
24 not in an active request, URLs will be external by default, but
25 this requires setting :data:`SERVER_NAME` so Flask knows what
26 domain to use. :data:`APPLICATION_ROOT` and
27 :data:`PREFERRED_URL_SCHEME` should also be configured as
28 needed. This config is only used when not in an active request.
29
30 Functions can be decorated with :meth:`url_defaults` to modify
31 keyword arguments before the URL is built.
32
33 If building fails for some reason, such as an unknown endpoint
34 or incorrect values, the app's :meth:`handle_url_build_error`
35 method is called. If that returns a string, that is returned,
36 otherwise a :exc:`~werkzeug.routing.BuildError` is raised.
37
38 :param endpoint: The endpoint name associated with the URL to
39 generate. If this starts with a ``.``, the current blueprint
40 name (if any) will be used.
41 :param _anchor: If given, append this as ``#anchor`` to the URL.
42 :param _method: If given, generate the URL associated with this
43 method for the endpoint.
44 :param _scheme: If given, the URL will have this scheme if it
45 is external.
46 :param _external: If given, prefer the URL to be internal
47 (False) or require it to be external (True). External URLs
48 include the scheme and domain. When not in an active
49 request, URLs are external by default.
50 :param values: Values to use for the variable parts of the URL
51 rule. Unknown keys are appended as query string arguments,
52 like ``?a=b&c=d``.
53
54 .. versionadded:: 2.2
55 Moved from ``flask.url_for``, which calls this method.
56 """
57 req_ctx = _cv_request.get(None)
58
59 if req_ctx is not None:
60 url_adapter = req_ctx.url_adapter
61 blueprint_name = req_ctx.request.blueprint
62
63 # If the endpoint starts with "." and the request matches a
64 # blueprint, the endpoint is relative to the blueprint.
65 if endpoint[:1] == ".":
66 if blueprint_name is not None:
67 endpoint = f"{blueprint_name}{endpoint}"
68 else:
69 endpoint = endpoint[1:]
70
71 # When in a request, generate a URL without scheme and
72 # domain by default, unless a scheme is given.
73 if _external is None:
74 _external = _scheme is not None
75 else:
76 app_ctx = _cv_app.get(None)
77
78 # If called by helpers.url_for, an app context is active,
79 # use its url_adapter. Otherwise, app.url_for was called
80 # directly, build an adapter.
81 if app_ctx is not None:
82 url_adapter = app_ctx.url_adapter
83 else:
84 url_adapter = self.create_url_adapter(None)
85
86 if url_adapter is None:
87 raise RuntimeError(
88 "Unable to build URLs outside an active request"
89 " without 'SERVER_NAME' configured. Also configure"
90 " 'APPLICATION_ROOT' and 'PREFERRED_URL_SCHEME' as"
91 " needed."
92 )
93
94 # When outside a request, generate a URL with scheme and
95 # domain by default.
96 if _external is None:
97 _external = True
98
99 # It is an error to set _scheme when _external=False, in order
100 # to avoid accidental insecure URLs.
101 if _scheme is not None and not _external:
102 raise ValueError("When specifying '_scheme', '_external' must be True.")
103
104 self.inject_url_defaults(endpoint, values)
105
106 try:
107 rv = url_adapter.build( # type: ignore[union-attr]
108 endpoint,
109 values,
110 method=_method,
111 url_scheme=_scheme,
112 force_external=_external,
113 )
114 except BuildError as error:
115 values.update(
116 _anchor=_anchor, _method=_method, _scheme=_scheme, _external=_external
117 )
118 return self.handle_url_build_error(error, endpoint, values)
119
120 if _anchor is not None:
121 _anchor = _url_quote(_anchor, safe="%!#$&'()*+,/:;=?@")
122 rv = f"{rv}#{_anchor}"
123
124 return rv
Path 7: 1 calls (0.02)
'index' (1)
'x y' (1)
None (1)
None (1)
None (1)
{} (1)
'/#x%20y' (1)
1def url_for(
2 self,
3 endpoint: str,
4 *,
5 _anchor: str | None = None,
6 _method: str | None = None,
7 _scheme: str | None = None,
8 _external: bool | None = None,
9 **values: t.Any,
10 ) -> str:
11 """Generate a URL to the given endpoint with the given values.
12
13 This is called by :func:`flask.url_for`, and can be called
14 directly as well.
15
16 An *endpoint* is the name of a URL rule, usually added with
17 :meth:`@app.route() <route>`, and usually the same name as the
18 view function. A route defined in a :class:`~flask.Blueprint`
19 will prepend the blueprint's name separated by a ``.`` to the
20 endpoint.
21
22 In some cases, such as email messages, you want URLs to include
23 the scheme and domain, like ``https://example.com/hello``. When
24 not in an active request, URLs will be external by default, but
25 this requires setting :data:`SERVER_NAME` so Flask knows what
26 domain to use. :data:`APPLICATION_ROOT` and
27 :data:`PREFERRED_URL_SCHEME` should also be configured as
28 needed. This config is only used when not in an active request.
29
30 Functions can be decorated with :meth:`url_defaults` to modify
31 keyword arguments before the URL is built.
32
33 If building fails for some reason, such as an unknown endpoint
34 or incorrect values, the app's :meth:`handle_url_build_error`
35 method is called. If that returns a string, that is returned,
36 otherwise a :exc:`~werkzeug.routing.BuildError` is raised.
37
38 :param endpoint: The endpoint name associated with the URL to
39 generate. If this starts with a ``.``, the current blueprint
40 name (if any) will be used.
41 :param _anchor: If given, append this as ``#anchor`` to the URL.
42 :param _method: If given, generate the URL associated with this
43 method for the endpoint.
44 :param _scheme: If given, the URL will have this scheme if it
45 is external.
46 :param _external: If given, prefer the URL to be internal
47 (False) or require it to be external (True). External URLs
48 include the scheme and domain. When not in an active
49 request, URLs are external by default.
50 :param values: Values to use for the variable parts of the URL
51 rule. Unknown keys are appended as query string arguments,
52 like ``?a=b&c=d``.
53
54 .. versionadded:: 2.2
55 Moved from ``flask.url_for``, which calls this method.
56 """
57 req_ctx = _cv_request.get(None)
58
59 if req_ctx is not None:
60 url_adapter = req_ctx.url_adapter
61 blueprint_name = req_ctx.request.blueprint
62
63 # If the endpoint starts with "." and the request matches a
64 # blueprint, the endpoint is relative to the blueprint.
65 if endpoint[:1] == ".":
66 if blueprint_name is not None:
67 endpoint = f"{blueprint_name}{endpoint}"
68 else:
69 endpoint = endpoint[1:]
70
71 # When in a request, generate a URL without scheme and
72 # domain by default, unless a scheme is given.
73 if _external is None:
74 _external = _scheme is not None
75 else:
76 app_ctx = _cv_app.get(None)
77
78 # If called by helpers.url_for, an app context is active,
79 # use its url_adapter. Otherwise, app.url_for was called
80 # directly, build an adapter.
81 if app_ctx is not None:
82 url_adapter = app_ctx.url_adapter
83 else:
84 url_adapter = self.create_url_adapter(None)
85
86 if url_adapter is None:
87 raise RuntimeError(
88 "Unable to build URLs outside an active request"
89 " without 'SERVER_NAME' configured. Also configure"
90 " 'APPLICATION_ROOT' and 'PREFERRED_URL_SCHEME' as"
91 " needed."
92 )
93
94 # When outside a request, generate a URL with scheme and
95 # domain by default.
96 if _external is None:
97 _external = True
98
99 # It is an error to set _scheme when _external=False, in order
100 # to avoid accidental insecure URLs.
101 if _scheme is not None and not _external:
102 raise ValueError("When specifying '_scheme', '_external' must be True.")
103
104 self.inject_url_defaults(endpoint, values)
105
106 try:
107 rv = url_adapter.build( # type: ignore[union-attr]
108 endpoint,
109 values,
110 method=_method,
111 url_scheme=_scheme,
112 force_external=_external,
113 )
114 except BuildError as error:
115 values.update(
116 _anchor=_anchor, _method=_method, _scheme=_scheme, _external=_external
117 )
118 return self.handle_url_build_error(error, endpoint, values)
119
120 if _anchor is not None:
121 _anchor = _url_quote(_anchor, safe="%!#$&'()*+,/:;=?@")
122 rv = f"{rv}#{_anchor}"
123
124 return rv
Path 8: 1 calls (0.02)
'index' (1)
None (1)
None (1)
'https' (1)
False (1)
{} (1)
ValueError (1)
1def url_for(
2 self,
3 endpoint: str,
4 *,
5 _anchor: str | None = None,
6 _method: str | None = None,
7 _scheme: str | None = None,
8 _external: bool | None = None,
9 **values: t.Any,
10 ) -> str:
11 """Generate a URL to the given endpoint with the given values.
12
13 This is called by :func:`flask.url_for`, and can be called
14 directly as well.
15
16 An *endpoint* is the name of a URL rule, usually added with
17 :meth:`@app.route() <route>`, and usually the same name as the
18 view function. A route defined in a :class:`~flask.Blueprint`
19 will prepend the blueprint's name separated by a ``.`` to the
20 endpoint.
21
22 In some cases, such as email messages, you want URLs to include
23 the scheme and domain, like ``https://example.com/hello``. When
24 not in an active request, URLs will be external by default, but
25 this requires setting :data:`SERVER_NAME` so Flask knows what
26 domain to use. :data:`APPLICATION_ROOT` and
27 :data:`PREFERRED_URL_SCHEME` should also be configured as
28 needed. This config is only used when not in an active request.
29
30 Functions can be decorated with :meth:`url_defaults` to modify
31 keyword arguments before the URL is built.
32
33 If building fails for some reason, such as an unknown endpoint
34 or incorrect values, the app's :meth:`handle_url_build_error`
35 method is called. If that returns a string, that is returned,
36 otherwise a :exc:`~werkzeug.routing.BuildError` is raised.
37
38 :param endpoint: The endpoint name associated with the URL to
39 generate. If this starts with a ``.``, the current blueprint
40 name (if any) will be used.
41 :param _anchor: If given, append this as ``#anchor`` to the URL.
42 :param _method: If given, generate the URL associated with this
43 method for the endpoint.
44 :param _scheme: If given, the URL will have this scheme if it
45 is external.
46 :param _external: If given, prefer the URL to be internal
47 (False) or require it to be external (True). External URLs
48 include the scheme and domain. When not in an active
49 request, URLs are external by default.
50 :param values: Values to use for the variable parts of the URL
51 rule. Unknown keys are appended as query string arguments,
52 like ``?a=b&c=d``.
53
54 .. versionadded:: 2.2
55 Moved from ``flask.url_for``, which calls this method.
56 """
57 req_ctx = _cv_request.get(None)
58
59 if req_ctx is not None:
60 url_adapter = req_ctx.url_adapter
61 blueprint_name = req_ctx.request.blueprint
62
63 # If the endpoint starts with "." and the request matches a
64 # blueprint, the endpoint is relative to the blueprint.
65 if endpoint[:1] == ".":
66 if blueprint_name is not None:
67 endpoint = f"{blueprint_name}{endpoint}"
68 else:
69 endpoint = endpoint[1:]
70
71 # When in a request, generate a URL without scheme and
72 # domain by default, unless a scheme is given.
73 if _external is None:
74 _external = _scheme is not None
75 else:
76 app_ctx = _cv_app.get(None)
77
78 # If called by helpers.url_for, an app context is active,
79 # use its url_adapter. Otherwise, app.url_for was called
80 # directly, build an adapter.
81 if app_ctx is not None:
82 url_adapter = app_ctx.url_adapter
83 else:
84 url_adapter = self.create_url_adapter(None)
85
86 if url_adapter is None:
87 raise RuntimeError(
88 "Unable to build URLs outside an active request"
89 " without 'SERVER_NAME' configured. Also configure"
90 " 'APPLICATION_ROOT' and 'PREFERRED_URL_SCHEME' as"
91 " needed."
92 )
93
94 # When outside a request, generate a URL with scheme and
95 # domain by default.
96 if _external is None:
97 _external = True
98
99 # It is an error to set _scheme when _external=False, in order
100 # to avoid accidental insecure URLs.
101 if _scheme is not None and not _external:
102 raise ValueError("When specifying '_scheme', '_external' must be True.")
103
104 self.inject_url_defaults(endpoint, values)
105
106 try:
107 rv = url_adapter.build( # type: ignore[union-attr]
108 endpoint,
109 values,
110 method=_method,
111 url_scheme=_scheme,
112 force_external=_external,
113 )
114 except BuildError as error:
115 values.update(
116 _anchor=_anchor, _method=_method, _scheme=_scheme, _external=_external
117 )
118 return self.handle_url_build_error(error, endpoint, values)
119
120 if _anchor is not None:
121 _anchor = _url_quote(_anchor, safe="%!#$&'()*+,/:;=?@")
122 rv = f"{rv}#{_anchor}"
123
124 return rv