Is there something similar to 'rake routes' in django?

PythonRuby on-RailsDjangoRoutingDjango Urls

Python Problem Overview

In rails, on can show the active routes with rake (

$ rake routes
          users GET  /users          {:controller=>"users", :action=>"index"}
formatted_users GET  /users.:format  {:controller=>"users", :action=>"index"}
                POST /users          {:controller=>"users", :action=>"create"}
                POST /users.:format  {:controller=>"users", :action=>"create"}

Is there a similar tool/command for django showing the e.g. the URL pattern, the name of the pattern (if any) and the associated function in the views?

Python Solutions

Solution 1 - Python

Have found

$ ./ show_urls

Solution 2 - Python

An experiment ...

# appended to root

if __name__ == '__main__':

    from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
    from django.utils.termcolors import colorize
    import os, sys

    os.environ['DJANGO_SETTINGS_MODULE'] = 'ialtr.settings'

    def traverse(url_patterns, prefix=''):
        for p in url_patterns:
            if isinstance(p, RegexURLPattern):
                composed = '%s%s' % (prefix, p.regex.pattern)
                composed = composed.replace('/^', '/')
                print colorize('\t%s' % (composed), fg='green'), '==> ',
                    sys.stdout.write(colorize('%s.' % p.callback.__module__,
                    print p.callback.func_name
                    print p.callback.__class__.__name__
            if isinstance(p, RegexURLResolver):
                traverse(p.url_patterns, prefix=p.regex.pattern)


Now, if one runs python ...

$ python
    ^users/activate/complete/$ ==> django.views.generic.simple.direct_to_template
    ^users/activate/(?P<activation_key>\w+)/$ ==> registration.views.activate
    ^users/register/$ ==> registration.views.register
    ^users/register/complete/$ ==> django.views.generic.simple.direct_to_template
    ^users/register/closed/$ ==> django.views.generic.simple.direct_to_template
    ^login/$ ==> django.contrib.auth.views.MethodDecoratorAdaptor
    ^logout/$ ==> django.contrib.auth.views.logout
    ^password/change/$ ==> django.contrib.auth.views.MethodDecoratorAdaptor
    ^password/change/done/$ ==> django.contrib.auth.views.password_change_done
    ^password/reset/$ ==> django.contrib.auth.views.MethodDecoratorAdaptor
    ^password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$ ==> django.contrib.auth.views.password_reset_confirm
    ^password/reset/complete/$ ==> django.contrib.auth.views.password_reset_complete
    ^password/reset/done/$ ==> django.contrib.auth.views.password_reset_done
    ^ialt/applications/$ ==> ialt.views.applications
    ^static/(?P<path>.*)$ ==> django.views.static.serve
    ^$ ==> django.views.generic.simple.direct_to_template
    ^about/ ==> django.views.generic.simple.direct_to_template

Solution 3 - Python

When I tried miku's answer, I got this error:

>django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

It looks like the problem comes from using django.contrib.admin.autodiscover() in my, so I can either comment that out, or load Django properly before dumping the URL's. Of course if I want to see the admin URL's in the mapping, I can't comment them out.

The way I found was to create a custom management command that dumps the urls.

# install this file in mysite/myapp/management/commands/
from import BaseCommand

from kive import urls

class Command(BaseCommand):
    help = "Dumps all URL's."
    def handle(self, *args, **options):
    def show_urls(self, urllist, depth=0):
        for entry in urllist:
            print ' '.join(("  " * depth, entry.regex.pattern,
                            entry.callback and entry.callback.__module__ or '',
                            entry.callback and entry.callback.func_name or ''))
            if hasattr(entry, 'url_patterns'):
                self.show_urls(entry.url_patterns, depth + 1)

Solution 4 - Python

admindocs has a similar feature. But it doesn't display URL names.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmikuView Question on Stackoverflow
Solution 1 - PythonmikuView Answer on Stackoverflow
Solution 2 - PythonmikuView Answer on Stackoverflow
Solution 3 - PythonDon KirkbyView Answer on Stackoverflow
Solution 4 - PythonmuhukView Answer on Stackoverflow