我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用django.urls.RegexURLPattern()。
def check_resolver(resolver): """ Recursively check the resolver. """ from django.urls import RegexURLPattern, RegexURLResolver warnings = [] for pattern in resolver.url_patterns: if isinstance(pattern, RegexURLResolver): warnings.extend(check_include_trailing_dollar(pattern)) # Check resolver recursively warnings.extend(check_resolver(pattern)) elif isinstance(pattern, RegexURLPattern): warnings.extend(check_pattern_name(pattern)) else: # This is not a url() instance warnings.extend(get_warning_for_invalid_pattern(pattern)) if not warnings: warnings.extend(check_pattern_startswith_slash(pattern)) return warnings
def get_all_views_from_patterns(url_patterns): """ Get a list of tuples containing (1) the URL pattern regex, (2) the pattern name, and (3) the callback function for all of the registered URLs in the given list of URL patterns. :param url_patterns: A list of URL patterns to process. :return: A list of tuples containing (1) the URL pattern regex, (2) the pattern name, and (3) the callback function for all of the registered URLs in the given list of URL patterns. """ to_return = [] for pattern in url_patterns: if isinstance(pattern, RegexURLResolver): to_return.extend(UrlPatternHelper.get_all_views_from_patterns(pattern.url_patterns)) elif isinstance(pattern, RegexURLPattern): to_return.append((pattern.regex, pattern.name, pattern.callback)) return to_return # Class Methods # Public Methods # Protected Methods # Private Methods # Properties # Representation and Comparison
def handle(self, *args, **options): from django.conf import settings root_urlconf = __import__(settings.ROOT_URLCONF) all_urlpatterns = root_urlconf.urls.urlpatterns views = [] def get_all_view_names(urlpatterns): for pattern in urlpatterns: if isinstance(pattern, RegexURLResolver): get_all_view_names(pattern.url_patterns) elif isinstance(pattern, RegexURLPattern): if hasattr(pattern.callback, "view_class"): views.append(pattern.callback.view_class) return views for view in views: if hasattr(view, "template_name") and view.template_name: print(view.template_name, ":", view.__name__) else: try: v = view() v.object = "foo" v.object_list = ["foo"] print(v.get_template_names()[0], ":", view.__name__) except Exception as e: print(view.__name__, "Err-------------------or:", e) links = [] templates = glob('*/templates/*/*.html') templates += glob('*/templates/*.html') regex = re.compile(r'''.*(href|post).*{% ?url (?P<quote>['"])(.*?)(?P=quote)(.*?)%}''') for t in templates: with open(t, 'r') as f: content = f.read() t = '/'.join(t.split('/')[2:]) links.extend([ Link(t, pattern, params.strip(), method) for (method, quotes, pattern, params) in regex.findall(content) ]) dot = Digraph() for link in links: try: from_node = template_to_view[link.template] except KeyError: print(link.template) from_node = "Unknown" url = reverse(link.pattern, args=link.args, kwargs=link.kwargs) to_node = resolve(url).func.__name__ dot.node(from_node, from_node) dot.node(to_node, to_node) if link.method == "post": dot.edge(from_node, to_node, color="lightgrey") else: dot.edge(from_node, to_node) dot.render("linkgraph.gv")