Trying to trace a circular import error in Django

隐身守侯 提交于 2019-12-20 19:36:57

问题


I understand circular import error has been asked about a lot but after going through these questions I haven't been able to solve my issue. When I try to run my server in Django its giving me this error message:

The included URLconf module 'accounts_app' from path\to\myproject\__init__.py 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.

The issue started when i added a new app which has a urls.py like the following

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^signin$', views.signin, name='signin'),
    url(r'^signout$', views.signout, name='signout'),
    url(r'^signup$', views.signup, name='signup'),
]

My project urls.py has a line which points to the app and looks like the following code

urlpatterns = [
     url(r'^accounts/', include('accounts_app')),
]

My view looks like the following:

from django.shortcuts import render
from django.http import HttpResponse

def signin(request):
    return HttpResponse("<p>This the signin view</p>")

def signout(request):
    return HttpResponse("<p>This the signout view</p>")

def signup(request):
    return HttpResponse("<p>This the signup view</p>")

Can anyone please help me identify were I could possibly be going wrong.


回答1:


Try changing

urlpatterns = [
     url(r'^accounts/', include('accounts_app')),
] 

to

urlpatterns = [
     url(r'^accounts/', include('accounts_app.urls')), # add .urls after app name
]



回答2:


for those who have the same error but still hasn't debugged their code, also check how you typed "urlpatterns"

having it mistyped or with dash/underscore will result to the same error




回答3:


Those habitual with CamelCased names may face the error as well.

urlpatterns has to be typed exactly as 'urlpatterns'

This will show you error -

urlPatterns = [
    path('', views.index, name='index'),

Error -

django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'polls.urls' from '...\\polls\\urls.py'>' 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.

However, fixing the CamelCase will work -

urlpatterns = [
    path('', views.index, name='index'),
]



回答4:


In my case I was getting error because I was giving wrong path of dir containing urls. So I changed this

urlpatterns = [
    url(r'^user/', include('core.urls'))
]

to this

urlpatterns = [
    url(r'^user/', include('core.urls.api'))
]



回答5:


In my case i was getting this error because i was typing urlpatterns in urls.py to urlpattern.




回答6:


This error also appears if SomeView doesn't exist in views.py and you do this:

from someapp import views

urlpatterns = [
    path('api/someurl/', views.SomeView.as_view(), name="someview"),
]

So make sure all Views you use in urls.py exist in views.py



来源:https://stackoverflow.com/questions/34465954/trying-to-trace-a-circular-import-error-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!