gpt4 book ai didi

django reverse 导致循环导入

转载 作者:行者123 更新时间:2023-12-03 07:35:09 25 4
gpt4 key购买 nike

我的项目 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应用程序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网址配置:
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配置文件
urlpatterns = patterns(
'report.views',
url(r'^$', views.LampReportView.as_view(), name='lamp_report'),
)

users配置文件
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.

那么这里的循环导入在哪里?我怎样才能避免它?

最佳答案

使用 reverse_lazy() 而不是 reverse() .

关于django reverse 导致循环导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30460461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com