views:

255

answers:

3

In rails, on can show the active routes with rake (http://guides.rubyonrails.org/routing.html):

$ 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?

A: 

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

muhuk
+3  A: 

An experiment ...

# appended to root urls.py

if __name__ == '__main__':

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

    sys.path.append(os.path.abspath('..'))
    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'), '==> ',
                try:
                    sys.stdout.write(colorize('%s.' % p.callback.__module__,
                        fg='yellow'))
                    print p.callback.func_name
                except:
                    print p.callback.__class__.__name__
            if isinstance(p, RegexURLResolver):
                traverse(p.url_patterns, prefix=p.regex.pattern)

    traverse(urlpatterns)

Now, if one runs python urls.py ...

$ python urls.py
    ^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

Source: http://github.com/miku/django-display-routes

The MYYN
+1  A: 

Have found http://code.google.com/p/django-command-extensions/:

$ ./manage.py show_urls
The MYYN