Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django reverse causes circular import

Tags:

django

I have this in my project 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) 

this is my 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)) ) 

this is 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),   ) 

and report urlconf

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

and users urlconf

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

now when I do

reverse('lamp_report') 

i get this:

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. 

so where is the circular import here? and how can I avoid it?

like image 893
kurtgn Avatar asked May 26 '15 13:05

kurtgn


People also ask

What causes circular import in Django?

Generally, the Python Circular Import problem occurs when you accidentally name your working file the same as the module name and those modules depend on each other. This way the python opens the same file which causes a circular loop and eventually throws an error.

What does reverse in Django do?

the reverse function allows to retrieve url details from url's.py file through the name value provided there. This is the major use of reverse function in Django. The redirect variable is the variable here which will have the reversed value. So the reversed url value will be placed here.


1 Answers

Use reverse_lazy() instead of reverse().

like image 136
knbk Avatar answered Oct 02 '22 04:10

knbk