MCPcopy
hub / github.com/django/django / reverse

Function reverse

django/urls/base.py:28–108  ·  django/urls/base.py::reverse
(
    viewname,
    urlconf=None,
    args=None,
    kwargs=None,
    current_app=None,
    *,
    query=None,
    fragment=None,
)

Source from the content-addressed store, hash-verified

26
27
28def reverse(
29 viewname,
30 urlconf=None,
31 args=None,
32 kwargs=None,
33 current_app=None,
34 *,
35 query=None,
36 fragment=None,
37):
38 if urlconf is None:
39 urlconf = get_urlconf()
40 resolver = get_resolver(urlconf)
41 args = args or []
42 kwargs = kwargs or {}
43
44 prefix = get_script_prefix()
45
46 if not isinstance(viewname, str):
47 view = viewname
48 else:
49 *path, view = viewname.split(class="st">":")
50
51 if current_app:
52 current_path = current_app.split(class="st">":")
53 current_path.reverse()
54 else:
55 current_path = None
56
57 resolved_path = []
58 ns_pattern = class="st">""
59 ns_converters = {}
60 for ns in path:
61 current_ns = current_path.pop() if current_path else None
62 class="cm"># Lookup the name to see if it could be an app identifier.
63 try:
64 app_list = resolver.app_dict[ns]
65 class="cm"># Yes! Path part matches an app in the current Resolver.
66 if current_ns and current_ns in app_list:
67 class="cm"># If we are reversing for a particular app, use that
68 class="cm"># namespace.
69 ns = current_ns
70 elif ns not in app_list:
71 class="cm"># The name isn't shared by one of the instances (i.e.,
72 class="cm"># the default) so pick the first instance as the default.
73 ns = app_list[0]
74 except KeyError:
75 pass
76
77 if ns != current_ns:
78 current_path = None
79
80 try:
81 extra, resolver = resolver.namespace_dict[ns]
82 resolved_path.append(ns)
83 ns_pattern += extra
84 ns_converters.update(resolver.pattern.converters)
85 except KeyError as key:

Callers 15

resolve_urlFunction · 0.90
user_change_passwordMethod · 0.90
get_absolute_urlMethod · 0.90
indexFunction · 0.90
locationMethod · 0.90
parse_rstFunction · 0.90
innerMethod · 0.90
password_changeMethod · 0.90
loginMethod · 0.90
_build_app_dictMethod · 0.90
get_view_on_site_urlMethod · 0.90
response_addMethod · 0.90

Calls 15

get_urlconfFunction · 0.85
get_resolverFunction · 0.85
NoReverseMatchClass · 0.85
get_ns_resolverFunction · 0.85
_reverse_with_prefixMethod · 0.80
urlencodeMethod · 0.80
get_script_prefixFunction · 0.70
urlencodeFunction · 0.50
splitMethod · 0.45
reverseMethod · 0.45
popMethod · 0.45
appendMethod · 0.45