| 26 | |
| 27 | |
| 28 | def 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: |