Last active
January 3, 2016 07:29
-
-
Save RickyCook/8429386 to your computer and use it in GitHub Desktop.
List a Django app's urlpatterns in a table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# General setup | |
# Import your app and set APP_URLS to your urlpatterns | |
# | |
import iss.urls | |
APP_URLS = iss.urls.urlpatterns | |
from url_levels import url_levels | |
out = url_levels(APP_URLS) | |
# | |
# IPython Notebook with pandas | |
# | |
import pandas | |
from IPython.display import HTML | |
HTML(pandas.DataFrame(out).to_html()) | |
# | |
# Terminal | |
# See https://github.com/RickyCook/pptable for pptable.py | |
# | |
from pptable import pptable | |
pptable(out) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
List a Django app's urlpatterns. | |
This is better, in some ways, than ./manage.py show_urls, as it shows | |
the full regexes that match. | |
""" | |
def url_levels(pat, prefix=None): | |
""" | |
Compile a list of all Django url patterns in a formatted array | |
""" | |
if prefix is None: | |
prefix = [] | |
try: | |
all = [] | |
loop_list = pat | |
if hasattr(pat, 'url_patterns'): | |
loop_list = pat.url_patterns | |
for url in loop_list: | |
all += url_levels(url, prefix + [url.regex.pattern]) | |
return all | |
except TypeError: | |
return [prefix] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment