<p>当我尝试<a href="https://stackoverflow.com/a/2094270/4794">miku's answer</a>时,出现了以下错误:</p>
<blockquote>
<p>django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.</p>
</blockquote>
<p>看起来问题来自于在我的<code>urls.py</code>中使用<code>django.contrib.admin.autodiscover()</code>,所以我可以在转储URL之前注释掉它,或者正确加载Django。当然,如果我想在映射中看到管理URL,我不能注释掉它们。</p>
<p>我找到的方法是创建一个<a href="https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/" rel="nofollow noreferrer">custom management command</a>来转储url。</p>
<pre><code># 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)
</code></pre>