MCPcopy
hub / github.com/django/django / build_absolute_uri

Method build_absolute_uri

django/http/request.py:263–300  ·  view source on GitHub ↗

Build an absolute URI from the location and the variables available in this request. If no ``location`` is specified, build the absolute URI using request.get_full_path(). If the location is absolute, convert it to an RFC 3987 compliant URI and return it. If location

(self, location=None)

Source from the content-addressed store, hash-verified

261 return value
262
263 def build_absolute_uri(self, location=None):
264 """
265 Build an absolute URI from the location and the variables available in
266 this request. If no ``location`` is specified, build the absolute URI
267 using request.get_full_path(). If the location is absolute, convert it
268 to an RFC 3987 compliant URI and return it. If location is relative or
269 is scheme-relative (i.e., ``//example.com/``), urljoin() it to a base
270 URL constructed from the request variables.
271 """
272 if location is None:
273 # Make it an absolute url (but schemeless and domainless) for the
274 # edge case that the path starts with '//'.
275 location = "//%s" % self.get_full_path()
276 else:
277 # Coerce lazy locations.
278 location = str(location)
279 bits = urlsplit(location)
280 if not (bits.scheme and bits.netloc):
281 # Handle the simple, most common case. If the location is absolute
282 # and a scheme or host (netloc) isn't provided, skip an expensive
283 # urljoin() as long as no path segments are '.' or '..'.
284 if (
285 bits.path.startswith("/")
286 and not bits.scheme
287 and not bits.netloc
288 and "/./" not in bits.path
289 and "/../" not in bits.path
290 ):
291 # If location starts with '//' but has no netloc, reuse the
292 # schema and netloc from the current request. Strip the double
293 # slashes and continue as if it wasn't specified.
294 location = self._current_scheme_host + location.removeprefix("//")
295 else:
296 # Join the constructed URL with the provided location, which
297 # allows the provided location to apply query strings to the
298 # base path.
299 location = urljoin(self._current_scheme_host + self.path, location)
300 return iri_to_uri(location)
301
302 @cached_property
303 def _current_scheme_host(self):

Callers 10

test_absolute_urlMethod · 0.95
test_host_retrievalMethod · 0.95
handle_no_permissionMethod · 0.80
_redirect_to_loginFunction · 0.80
handle_no_permissionMethod · 0.80
_get_preserved_qslMethod · 0.80
_generate_cache_keyFunction · 0.80

Calls 2

get_full_pathMethod · 0.95
iri_to_uriFunction · 0.90

Tested by 4

test_absolute_urlMethod · 0.76
test_host_retrievalMethod · 0.76