Skip to content

Commit 4abfbd5

Browse files
authored
rewrite build docstring (#3097)
2 parents 86e11c2 + 161c18b commit 4abfbd5

1 file changed

Lines changed: 40 additions & 63 deletions

File tree

src/werkzeug/routing/map.py

Lines changed: 40 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -834,69 +834,46 @@ def build(
834834
append_unknown: bool = True,
835835
url_scheme: str | None = None,
836836
) -> str:
837-
"""Building URLs works pretty much the other way round. Instead of
838-
`match` you call `build` and pass it the endpoint and a dict of
839-
arguments for the placeholders.
840-
841-
The `build` function also accepts an argument called `force_external`
842-
which, if you set it to `True` will force external URLs. Per default
843-
external URLs (include the server name) will only be used if the
844-
target URL is on a different subdomain.
845-
846-
>>> m = Map([
847-
... Rule('/', endpoint='index'),
848-
... Rule('/downloads/', endpoint='downloads/index'),
849-
... Rule('/downloads/<int:id>', endpoint='downloads/show')
850-
... ])
851-
>>> urls = m.bind("example.com", "/")
852-
>>> urls.build("index", {})
853-
'/'
854-
>>> urls.build("downloads/show", {'id': 42})
855-
'/downloads/42'
856-
>>> urls.build("downloads/show", {'id': 42}, force_external=True)
857-
'http://example.com/downloads/42'
858-
859-
Because URLs cannot contain non ASCII data you will always get
860-
bytes back. Non ASCII characters are urlencoded with the
861-
charset defined on the map instance.
862-
863-
Additional values are converted to strings and appended to the URL as
864-
URL querystring parameters:
865-
866-
>>> urls.build("index", {'q': 'My Searchstring'})
867-
'/?q=My+Searchstring'
868-
869-
When processing those additional values, lists are furthermore
870-
interpreted as multiple values (as per
871-
:py:class:`werkzeug.datastructures.MultiDict`):
872-
873-
>>> urls.build("index", {'q': ['a', 'b', 'c']})
874-
'/?q=a&q=b&q=c'
875-
876-
Passing a ``MultiDict`` will also add multiple values:
877-
878-
>>> urls.build("index", MultiDict((('p', 'z'), ('q', 'a'), ('q', 'b'))))
879-
'/?p=z&q=a&q=b'
880-
881-
If a rule does not exist when building a `BuildError` exception is
882-
raised.
883-
884-
The build method accepts an argument called `method` which allows you
885-
to specify the method you want to have an URL built for if you have
886-
different methods for the same endpoint specified.
887-
888-
:param endpoint: the endpoint of the URL to build.
889-
:param values: the values for the URL to build. Unhandled values are
890-
appended to the URL as query parameters.
891-
:param method: the HTTP method for the rule if there are different
892-
URLs for different methods on the same endpoint.
893-
:param force_external: enforce full canonical external URLs. If the URL
894-
scheme is not provided, this will generate
895-
a protocol-relative URL.
896-
:param append_unknown: unknown parameters are appended to the generated
897-
URL as query string argument. Disable this
898-
if you want the builder to ignore those.
899-
:param url_scheme: Scheme to use in place of the bound
837+
"""Build a URL by filling in any variable slots in the endpoint's rule
838+
with the provided values.
839+
840+
This produces relative URLs (path only, no host) by default, which is
841+
appropriate for most use cases. When using subdomain or host matching,
842+
the URL will be absolute if the endpoint is not on the same host as the
843+
current request. If you need an absolute URL no matter what, such as
844+
when adding a link to an email, pass ``force_external=True``.
845+
846+
Characters that are not allowed directly in URLs will be percent-encoded
847+
using UTF-8.
848+
849+
Additional values that don't match any rule parts are added to the query
850+
(``?key=value``) part of the URL.
851+
852+
If a rule is not found, an :exc:`.BuildError` is raised. Converters may
853+
also raise exceptions when preparing a value.
854+
855+
Unlike matching, building does not perform comprehensive validation of
856+
the values, it's assumed that they are trusted and correct. It's also
857+
possible to build a URL for one endpoint that will be matched by
858+
another, or will not match. This is very unlikely to happen or to matter
859+
in most applications, especially with testing. If you need a
860+
stronger guarantee, you can use a helper to verify that
861+
``match(build(endpoint)) == endpoint``.
862+
863+
:param endpoint: The endpoint for the rule to build.
864+
:param values: Values for the variable parts of the rule, which will be
865+
converted using the part's converter's
866+
:meth:`~.BaseConverter.to_url` method. Keys that don't match
867+
variable parts will be converted to a query string unless
868+
``append_unknown`` is disabled.
869+
:param method: Further match the rule to build by this HTTP method, for
870+
when there are multiple rules for the same endpoint that vary based
871+
on method.
872+
:param force_external: Always produce an absolute URL, for external use.
873+
If ``scheme`` is not set, this produces a protocol-relative URL.
874+
:param append_unknown: Convert values that don't match any rule parts
875+
to a query string. Disable to ignore such values.
876+
:param url_scheme: Scheme to use for absolute URLs. Defaults to
900877
:attr:`url_scheme`.
901878
902879
.. versionchanged:: 2.0

0 commit comments

Comments
 (0)