django快速獲取專案所有的URL

2021-07-25 20:35:02 字數 1937 閱讀 7939

django1.10快速獲取專案所有的url列表,可以用於許可權控制

函式如下:

import re

defget_url

(urllist , parent='' , depth=0 , url_list= , url_dict={}):

for entry in urllist:

url = entry.regex.pattern

if re.search('\(' , url) :

continue

#過濾正規表示式的url位址

url = url.replace(r'^' , '')

url = url.replace(r'$' , '')

url = url.replace(r'/' + '$' , '')

if depth == 0:

if url in ['admin' , 'admin/' , 'auth' , 'auth/']:

continue

if re.search('\/$' , url) :

else :

try :

except :

url_dict['/'] =

else :

try :

except :

url_dict['/' + parent] =

if hasattr(entry, 'url_patterns'):

get_url(entry.url_patterns , url , depth + 1)

# url_dict =

# url_list = ['/basicdata/', '/basicdata/department/export', '/basicdata/department/list', '/basicdata/department/detail', '/basicdata/department/edit', '/login.html', '/index.html']

return url_dict

注意:

呼叫get_url函式獲得清單,必須使用函式或者class來封裝

def

recreate

(request):

from library.django.get_url import get_url

from lykops import urls

url_dict = get_url(urls.urlpatterns)

print(url_dict)

return httpresponseredirect(reverse('list'))

然後修改urls.py

urlpatterns = [

......

url(r'^/recreate/', views.recreate, name="recreate"),

......

]

如果其他函式呼叫的話,報錯attributeerror: module 『lykops.urls』 has no attribute 『urlpatterns』

file "/opt/lykops/sysadmin/urls.py", line 5, in

file "/opt/lykops/sysadmin/views.py", line 76, in __init__

self.recreate()

file "/opt/lykops/sysadmin/views.py", line 79, in recreate

url_dict = get_allurl(urls.urlpatterns)

attributeerror: module 'lykops.urls' has no attribute 'urlpatterns'

獲取Redis所有的鍵值對

redis命令級別實現 scan cursor match pattern count count 1spring data redis實現 override public list getallredisval1 zset型別的鍵值獲取 if redistemplate.type key code...

獲取Django所有路由

直接上 from django.urls import regexurlpattern defget all urls patterns,pre fix,is firt time false,result if is firt time result.clear for item in patter...

獲取所有的預設值列表

獲取所有的預設值列表 select object name c.id as 表名 c.name as 欄位名 t.name as 資料型別 c.prec as 長度 p.value as 字段說明 m.text as 預設值 from syscolumns c inner join systypes...