django2 獲取專案中所有url

max_tian發表於2019-02-16

show me the code…

from django.urls import URLPattern


def get_all_urls(patterns, pre_fix, result):

    for item in patterns:
        part = item.pattern.regex.pattern.strip("^$")
        if isinstance(item, URLPattern):
            result.append(pre_fix + part)
        else:
            get_all_urls(item.url_patterns, pre_fix + part, result=result)
    return result

if __name == "__main__":
    # 專案根路由
    from server import urls
    print(get_all_urls(urls.urlpatterns, pre_fix="/", result=[]))

by max.

相關文章