In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Django3.2 automatic discovery of all routes how to achieve", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Django3.2 automatic discovery of all routes how to achieve" it!
1. Demand
Discover all local URL with aliases in the project to form an ordered dictionary to facilitate subsequent calls
Direct exclusion of some URL, such as routing under admin that comes with Django
two。 Code implementation import re # regular from collections import OrderedDict # create ordered dictionary from django.conf import settings # import setting configuration file (get project root path urls.py) from django.utils.module_loading import import_string # string import module from django.urls import URLPattern, URLResolver # Django custom class You can determine whether the current URL is the root path (URLPattern) or continue to distribute (URLResolver) def check_url_exclude (url): "" exclude some specific URL: param url: URL: return: "for regex in settings.AUTO_DISCOVER_EXCLUDE: # the URL to be targeted for exclusion (which can contain regularities) is written to the configuration file settings in the form of a list If re.match (regex) under .auto _ DISCOVER_EXCLUDE Url): return True "" example: AUTO_DISCOVER_EXCLUDE = ['/ admin/.*','/ login/','/ logout/','/ index/',] "" def recursion_urls (pre_namespace, pre_url, urlpatterns, url_ordered_dict): "" Recursively get the URL: param pre_namespace: namespace prefix For stitching the name: param pre_url: url prefix, for stitching url: param urlpatterns: routing relationship list: param url_ordered_dict: used to save all routes obtained in recursion: return: "for item in urlpatterns: if isinstance (item, URLPattern): # is already the root URL Get name and url write url_ordered_dict if not item.name: # routing addresses without aliases (name) skip continue # splicing route aliases (including distributed namespace) For example, "rbac:menu_list") if pre_namespace: name = "% slug% s"% (pre_namespace, item.name) else: name = item.name # splicing routing address URl (including upper-level routes distributed For example, "/ rbac/menu/list") url = pre_url + item.pattern.regex.pattern # at this time, the spliced route contains a start and stop symbol For example, / ^ rbac/ ^ menu / list/$ url = url.replace ("^", ") .replace (" $") "") # Delete the start and stop character: / rbac/menu/list/ # exclude some specific routes URL if check_url_exclude (url): # call the check_url_exclude function to directly exclude part of the URL continue url_ordered_ [name] = {"name": name "url": url} elif isinstance (item, URLResolver): # Route Distribution Recursive operation if pre_namespace: # whether the distribution in the last loop (one layer above) contains namespace if item.namespace: # whether this cycle (current layer) contains namespace namespace = "% SVA% s"% (pre_namespace) Item.namespace,) # both the upper layer and the current layer contain namespace else that splices two layers directly: namespace = pre_namespace # the current layer distribution does not include namespace Directly use the upper layer else: if item.namespace: namespace = item.namespace # the upper layer distribution does not include namespace Directly use the else of the current layer: namespace = None # neither the previous layer nor the current layer Directly define the layer none recursion_urls (namespace, pre_url + item.pattern.regex.pattern, item.url_patterns) Url_ordered_dict) # Recursive execution of def get_all_url_dict (): "get all routes of the project: return:" url_ordered_dict = OrderedDict () # an ordered dictionary md = import_string (settings.ROOT_URLCONF) containing all permissions URl of this project # the ROOT_URLCONF in the configuration file is the path (string) of the root routing urls.py for this project Using import_string, the string loading module recursion_urls (None, "/", md.urlpatterns, url_ordered_dict) # calls the recursion_urls function to get all routing dictionaries. No namespace under the root path is defined as None. No url prefix is defined as / return url_ordered_dict Thank you for your reading, the above is the content of "Django3.2 automatically discovers all routes". After the study of this article, I believe you have a deeper understanding of how Django3.2 automatically discovers all routes, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.