我有一个项目(myproj),里面有几个应用程序。一个应用程序(myapp)有一个我正在努力的管理命令。我希望此命令发送一封电子邮件,其中包含管理站点上的模型实例的URL。但我一直收到此错误ImproperlyConfigured: The included urlconf XXX doesn't have any patterns in it
(其中XXX是某个数字)。
其他一切似乎都有效。当我访问'/ admin / myapp'页面时,我的所有模型都会出现,我可以执行其中的所有操作。因此,我的网址模式似乎并不是一个错误。我有一种感觉,管理命令没有加载url模式,这就是它失败的原因,但我无法弄清楚如何让它做到这一点。
一些缩写代码。全部在python 2.7.6上。 Django 1.5.5
from __future__ import absolute_import, unicode_literals
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^auth/', include('django.contrib.auth.urls')),
url(r'^admin/', include(admin.site.urls)),
# Other app includes, but none for myapp as it is strictly admin
)
from __future__ import absolute_import, unicode_literals
from django.conf.urls import patterns, url
urlpatterns = patterns('',
)
from __future__ import absolute_import, unicode_literals
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
def handle(self, *args, **options):
from ... import do_stuff
do_stuff()
from __future__ import absolute_import, unicode_literals
# Tried: also reverse_lazy, just delays the exception
from django.core.urlresolvers import reverse
def do_stuff():
# Tried here: import myproj.urls
# Work...
# Exception here
a_url = reverse('admin:myapp_mymodel_change', a_model.pk)
# Work...
# Send email here
# Exception here if using reverse_lazy
答案 0 :(得分:1)
精氨酸。我是个白痴。应该是:
reverse('admin:myapp_mymodel_change', args=(a_model.pk,))