其他分享
首页 > 其他分享> > django路由分发1中RegexURLResolver, RegexURLPattern和2中URLResolver, URLPattern

django路由分发1中RegexURLResolver, RegexURLPattern和2中URLResolver, URLPattern

作者:互联网

话不多说,直接上代码

 

from django.conf import settings
from django.utils.module_loading import import_string
from django.urls import RegexURLResolver, RegexURLPattern
from collections import OrderedDict



def recursion_urls(pre_namespace, pre_url, urlpatterns, url_ordered_dict):
#None, "/", urlpatterns, url_ordered_dict

# 递归第二层
#'rbac', "/", rbac.urls.urlpatterns, url_ordered_dict

for item in urlpatterns: #w eb.urls.urlpatterns
'''
[
# url(r'^login/$', auth.login,name='login'), -- RegexURLPattern
url(r'^role/list/', views.role_list,name='role_list'),

]
'''
if isinstance(item, RegexURLResolver):
if pre_namespace:
if item.namespace:
namespace = "%s:%s" % (pre_namespace, item.namespace,) #'rbac':'rbac'
else:
namespace = pre_namespace # 'rbac'
else:
if item.namespace: #'rbac'
namespace = item.namespace #'rbac'
else:
namespace = None
# None, "/^", urlpatterns, url_ordered_dict
recursion_urls(namespace, pre_url + item.regex.pattern, item.url_patterns, url_ordered_dict)
#'/'+'^'--'/^',
else:

if pre_namespace: #None
name = "%s:%s" % (pre_namespace, item.name,)
else:
name = item.name #name = 'login'
if not item.name:
raise Exception('URL路由中必须设置name属性')

url = pre_url + item._regex #'^login/$'-- #/^^login/$
url_ordered_dict[name] = {'url_name': name, 'url': url.replace('^', '').replace('$', '')} #
#url_ordered_dict['login'] = {'name': 'login', 'url': url.replace('^', '').replace('$', '')} #/login/

def get_all_url_dict(ignore_namespace_list=None):
"""
获取路由中
:return:
"""
ignore_list = ignore_namespace_list or []
#ignore_list -- ['admin', ]

#存放项目所有 url路由用的有序字典
url_ordered_dict = OrderedDict()

md = import_string(settings.ROOT_URLCONF)

urlpatterns = []
# urlpatterns = [
# url(r'^', include('web.urls')),
# url(r'^rbac/', include('rbac.urls', namespace='rbac')),
# ]

print(md.urlpatterns)
for item in md.urlpatterns:
"""
[

url(r'^', include('web.urls')),--RegexURLResolver
url(r'^rbac/', include('rbac.urls',namespace='rbac')), -- RegexURLResolver
url(r'^xx', auth.xx,name='xx') -- RegexURLPattern
]

"""
# [RegexURLResolver,RegexURLResolver,RegexURLPattern]
if isinstance(item, RegexURLResolver) and item.namespace in ignore_list:
continue
urlpatterns.append(item)
recursion_urls(None, "/", urlpatterns, url_ordered_dict)
print('>>>>',url_ordered_dict)
return url_ordered_dict
这是django1.11版本的写法,最近我换成了django2.2,发现用法不一样。
from django.conf import settings
from django.utils.module_loading import import_string
from django.urls import URLPattern, URLResolver # 路由分发:URLResolver 不是路由分发:URLPattern
from collections import OrderedDict



def recursion_urls(pre_namespace, pre_url, urlpatterns, url_ordered_dict):
#None, "/", urlpatterns, url_ordered_dict

# 递归第二层
#'rbac', "/", rbac.urls.urlpatterns, url_ordered_dict

for item in urlpatterns: #w eb.urls.urlpatterns

if isinstance(item, URLResolver):
if pre_namespace:
if item.namespace:
namespace = "%s:%s" % (pre_namespace, item.namespace,) #'rbac':'rbac'
else:
namespace = pre_namespace # 'rbac'
else:
if item.namespace: #'rbac'
namespace = item.namespace #'rbac'
else:
namespace = None
# None, "/^", urlpatterns, url_ordered_dict
recursion_urls(namespace, pre_url + item.pattern.regex.pattern, item.url_patterns, url_ordered_dict)
#'/'+'^'--'/^',
else:

if pre_namespace: #None
name = "%s:%s" % (pre_namespace, item.name,)
else:
name = item.name #name = 'login'
if not item.name:
raise Exception('URL路由中必须设置name属性')

url = pre_url + item.pattern.regex.pattern #'^login/$'-- #/^^login/$
url_ordered_dict[name] = {'url_name': name, 'url': url.replace('^', '').replace('$', '')} #
#url_ordered_dict['login'] = {'name': 'login', 'url': url.replace('^', '').replace('$', '')} #/login/

def get_all_url_dict(ignore_namespace_list=None):
"""
获取路由中
:return:
"""
ignore_list = ignore_namespace_list or []
#ignore_list -- ['admin', ]

#存放项目所有 url路由用的有序字典
url_ordered_dict = OrderedDict()

md = import_string(settings.ROOT_URLCONF)

urlpatterns = []
# urlpatterns = [
# url(r'^', include('web.urls')),
# url(r'^rbac/', include('rbac.urls', namespace='rbac')),
# ]

print(md.urlpatterns)
for item in md.urlpatterns:

if isinstance(item, URLResolver) and item.namespace in ignore_list:
continue
urlpatterns.append(item)
recursion_urls(None, "/", urlpatterns, url_ordered_dict)
print('>>>>',url_ordered_dict)
return url_ordered_dict
总体上变化不大,变成了
URLPattern, URLResolver
url = pre_url + item.pattern.regex.pattern  # /^rbac/^user/edit/(?P<pk>\d_+)/$
recursion_urls(namespace, pre_url + item.pattern.regex.pattern, item.url_patterns, url_ordered_dict)
recursion_urls(None, '/', urlpatterns, url_ordered_dict)  # 递归的去获取所有的路由。根目录没有namespace,根路由用/
 

 

标签:RegexURLResolver,URLResolver,url,RegexURLPattern,namespace,item,dict,urlpatterns
来源: https://www.cnblogs.com/zzj666/p/14747266.html