{"id":108,"date":"2011-09-06T19:33:05","date_gmt":"2011-09-06T19:33:05","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/rprasad\/?p=108"},"modified":"2011-09-06T19:51:09","modified_gmt":"2011-09-06T19:51:09","slug":"django-admin-list-your-projects-url-names","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/rprasad\/2011\/09\/06\/django-admin-list-your-projects-url-names\/","title":{"rendered":"django urls: list a project&#8217;s url names"},"content":{"rendered":"<p>Below is quick hack to pull out an array of all the <a href=\"https:\/\/docs.djangoproject.com\/en\/dev\/topics\/http\/urls\/#naming-url-patterns\">url names<\/a> in a project.<\/p>\n<p>The script iterates through the &#8220;urlpatterns&#8221; defined in your settings file as the <a href=\"https:\/\/docs.djangoproject.com\/en\/dev\/ref\/settings\/#root-urlconf\">ROOT_URLCONF<\/a>.<\/p>\n<p>From the &#8220;python manage.py shell&#8221; prompt:<\/p>\n<pre>from django.conf import settings\r\n\r\nURL_NAMES = []\r\ndef load_url_pattern_names(patterns):\r\n    \"\"\"Retrieve a list of urlpattern names\"\"\"\r\n    global URL_NAMES\r\n    for pat in patterns:\r\n        if pat.__class__.__name__ == 'RegexURLResolver':            # load patterns from this RegexURLResolver\r\n            load_url_pattern_names(pat.url_patterns)\r\n        elif pat.__class__.__name__ == 'RegexURLPattern':           # load name from this RegexURLPattern\r\n            if pat.name is not None and pat.name not in URL_NAMES:\r\n                URL_NAMES.append( pat.name)\r\n    return URL_NAMES\r\n\r\nroot_urlconf = __import__(settings.ROOT_URLCONF)        # access the root urls.py file\r\nprint load_url_pattern_names(root_urlconf.urls.urlpatterns)   # access the \"urlpatterns\" from the ROOT_URLCONF<\/pre>\n<p>There&#8217;s probably an easier way to this, but I needed the url names for an error checking script.<\/p>\n<p><a href=\"http:\/\/blogs.law.harvard.edu\/rprasad\/files\/2011\/09\/Screen-shot-2011-09-06-at-3.49.02-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-124\" src=\"http:\/\/blogs.law.harvard.edu\/rprasad\/files\/2011\/09\/Screen-shot-2011-09-06-at-3.49.02-PM-300x150.png\" alt=\"\" width=\"300\" height=\"150\" srcset=\"https:\/\/archive.blogs.harvard.edu\/rprasad\/files\/2011\/09\/Screen-shot-2011-09-06-at-3.49.02-PM-300x150.png 300w, https:\/\/archive.blogs.harvard.edu\/rprasad\/files\/2011\/09\/Screen-shot-2011-09-06-at-3.49.02-PM.png 817w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is quick hack to pull out an array of all the url names in a project. The script iterates through the &#8220;urlpatterns&#8221; defined in your settings file as the ROOT_URLCONF. From the &#8220;python manage.py shell&#8221; prompt: from django.conf import settings URL_NAMES = [] def load_url_pattern_names(patterns): &#8220;&#8221;&#8221;Retrieve a list of urlpattern names&#8221;&#8221;&#8221; global URL_NAMES for &hellip; <a href=\"https:\/\/archive.blogs.harvard.edu\/rprasad\/2011\/09\/06\/django-admin-list-your-projects-url-names\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">django urls: list a project&#8217;s url names<\/span><\/a><\/p>\n","protected":false},"author":3875,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[50627,2671],"tags":[],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-django","category-urls"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4JC3p-1K","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/users\/3875"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":17,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":114,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts\/108\/revisions\/114"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}