在Django有类似'rake routes'的东西吗?

时间:2010-01-19 10:02:46

标签: python ruby-on-rails django routing django-urls

在rails中,on可以使用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"}

django是否有类似的工具/命令显示例如URL模式,模式的名称(如果有的话)和视图中的相关功能?

4 个答案:

答案 0 :(得分:53)

答案 1 :(得分:4)

实验......

# 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)

现在,如果运行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

答案 2 :(得分:3)

当我尝试miku's answer时,我收到了这个错误:

  

django.core.exceptions.AppRegistryNotReady:尚未加载应用。

看起来问题来自于在我的django.contrib.admin.autodiscover()中使用urls.py,因此我可以对其进行评论,或者在转储URL之前正确加载Django。当然,如果我想在映射中看到管理员URL,我就不能对它们进行评论。

我找到的方法是创建一个转储网址的custom management command

# install this file in mysite/myapp/management/commands/urldump.py
from django.core.management.base import BaseCommand

from kive import urls


class Command(BaseCommand):
    help = "Dumps all URL's."

    def handle(self, *args, **options):
        self.show_urls(urls.urlpatterns)

    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)

答案 3 :(得分:1)

admindocs也有类似的功能。但它不显示URL名称。