代码之家  ›  专栏  ›  技术社区  ›  miku

有没有类似的“耙路线”在Django?[副本]

  •  32
  • miku  · 技术社区  · 14 年前

    这个问题已经有了答案:

    在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 回复  |  直到 9 年前
        1
  •  48
  •   Jonah    11 年前
        2
  •  4
  •   miku    13 年前

    一个实验…

    # 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
    
        3
  •  3
  •   Community Egal    7 年前

    当我尝试 miku's answer ,我得到这个错误:

    django.core.exceptions.appRegistryNotReady:应用程序尚未加载。

    问题似乎来自于使用 django.contrib.admin.autodiscover() 在我的 urls.py ,所以我可以在转储URL之前将其注释掉,或者正确加载django。当然,如果我想在映射中看到管理URL,我不能将其注释掉。

    我发现的方法是创建一个 custom management command 这将转储URL。

    # 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)
    
        4
  •  1
  •   muhuk    14 年前

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