django反向导致循环导入

时间:2015-05-26 13:48:14

标签: django

我在我的项目urlconf photocheck.urls中有这个:

urlpatterns = patterns('',

    url(r'^admin/docs/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^rest/', include('core.urls')),
    url(r'^shotmaker/', include('shotmaker.urls')),
    url(r'^report/', include('report.urls')),
    url(r'^users/', include('users.urls')),

) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是我的core app urlconf:

router.register(r'cameras', views.CameraViewSet)
router.register(r'lamps', views.LampViewSet)
router.register(r'snapshots', views.SnapshotViewSet)

urlpatterns = patterns(
    'core.views',
    url(r'', include(router.urls))
)

这是shotmaker urlconf:

urlpatterns = patterns(
    'shotmaker.views',

    url(r'^$', views.CameraList.as_view(), name='camera_list'),
    url(r'^camera/(?P<pk>[-\w]+)/$', views.CameraDetail.as_view(), name='camera_detail'),
    url(r'^save_preview_image/(?P<pk>[-\w]+)/$', views.save_preview_image),
    url(r'^get_position/(?P<pk>[-\w]+)/$', views.get_position),
    url(r'^set_position/(?P<pk>[-\w]+)/$', views.set_position),
    url(r'^update_calibrating_image/(?P<pk>[-\w]+)/$', views.update_calibrating_image),
    url(r'^save_preview_get_position/(?P<pk>[-\w]+)/$', views.save_preview_get_position),


)

report urlconf

urlpatterns = patterns(
    'report.views',
    url(r'^$', views.LampReportView.as_view(), name='lamp_report'),
)

users urlconf

urlpatterns = patterns('',
url(r'^login/$', views.MyLoginView.as_view(), name="login"),
url(r'^logout/$', LogoutView.as_view(), name="logout"),
)

现在我做的时候

reverse('lamp_report')

我明白了:

Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "/Users/1111/.virtualenvs/gost_photo/lib/python2.7/site-packages/django/core/urlresolvers.py", line 546, in reverse
        return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
      File "/Users/1111/.virtualenvs/gost_photo/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in _reverse_with_prefix
        self._populate()
      File "/Users/1111/.virtualenvs/gost_photo/lib/python2.7/site-packages/django/core/urlresolvers.py", line 269, in _populate
        for pattern in reversed(self.url_patterns):
      File "/Users/1111/.virtualenvs/gost_photo/lib/python2.7/site-packages/django/core/urlresolvers.py", line 367, in url_patterns
        patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
      File "/Users/1111/.virtualenvs/gost_photo/lib/python2.7/site-packages/django/core/urlresolvers.py", line 361, in urlconf_module
        self._urlconf_module = import_module(self.urlconf_name)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
        __import__(name)
      File "/Users/1111/_gost/photo/photo-monitoring/photocheck/urls.py", line 15, in <module>
        url(r'^users/', include('users.urls')),
      File "/Users/1111/.virtualenvs/gost_photo/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 28, in include
        urlconf_module = import_module(urlconf_module)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
        __import__(name)
      File "/Users/1111/_gost/photo/photo-monitoring/users/urls.py", line 4, in <module>
        import views
      File "/Users/1111/_gost/photo/photo-monitoring/users/views.py", line 6, in <module>
        class MyLoginView(LoginView):
      File "/Users/1111/_gost/photo/photo-monitoring/users/views.py", line 8, in MyLoginView
        success_url = reverse('lamp_report')
      File "/Users/1111/.virtualenvs/gost_photo/lib/python2.7/site-packages/django/core/urlresolvers.py", line 546, in reverse
        return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
      File "/Users/1111/.virtualenvs/gost_photo/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in _reverse_with_prefix
        self._populate()
      File "/Users/1111/.virtualenvs/gost_photo/lib/python2.7/site-packages/django/core/urlresolvers.py", line 269, in _populate
        for pattern in reversed(self.url_patterns):
      File "/Users/1111/.virtualenvs/gost_photo/lib/python2.7/site-packages/django/core/urlresolvers.py", line 376, in url_patterns
        raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
    ImproperlyConfigured: The included urlconf 'photocheck.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

那么这里的循环导入在哪里?我该如何避免呢?

2 个答案:

答案 0 :(得分:33)

使用reverse_lazy()代替reverse()

答案 1 :(得分:4)

我收到同样的错误并已解决,但只有reverse_lazy()是不够的,请reverse_lazy()使用reverse_lazy('app_name:url_name')