Python 试图在 Django 中跟踪循环导入错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34465954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to trace a circular import error in Django
提问by Tatenda
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:
我知道很多人都问了循环导入错误,但是在解决了这些问题之后,我还是没能解决我的问题。当我尝试在 Django 中运行我的服务器时,它给了我这个错误消息:
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.
path\to\myproject\__init__.py 中包含的 URLconf 模块“accounts_app”似乎没有任何模式。如果您在文件中看到有效模式,则问题可能是由循环导入引起的。
The issue started when i added a new app which has a urls.py like the following
当我添加一个新的应用程序时,问题就开始了,该应用程序的 urls.py 如下所示
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
我的项目 urls.py 有一行指向应用程序,如下所示
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.
任何人都可以帮我确定我可能会出错。
采纳答案by Rahul Gupta
Try changing
尝试改变
urlpatterns = [
url(r'^accounts/', include('accounts_app')),
]
to
到
urlpatterns = [
url(r'^accounts/', include('accounts_app.urls')), # add .urls after app name
]
回答by Zohab Ali
In my case I was getting error because I was giving wrong path of dir containing urls. So I changed this
在我的情况下,我收到错误,因为我给出了包含 url 的错误路径。所以我改变了这个
urlpatterns = [
url(r'^user/', include('core.urls'))
]
to this
对此
urlpatterns = [
url(r'^user/', include('core.urls.api'))
]
回答by dani-cs
for those who have the same error but still hasn't debugged their code, also check how you typed "urlpatterns"
对于那些有同样错误但仍然没有调试他们的代码的人,还要检查你是如何输入“urlpatterns”的
having it mistyped or with dash/underscore will result to the same error
输入错误或使用破折号/下划线将导致相同的错误
回答by nitinr708
Those habitual with CamelCased names may face the error as well.
那些习惯使用 CamelCased 名称的人也可能面临这个错误。
urlpatterns
has to be typed exactly as 'urlpatterns'
urlpatterns
必须完全输入为“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 -
但是,修复 CamelCase 将起作用-
urlpatterns = [
path('', views.index, name='index'),
]
回答by Sonam gupta
In my case i was getting this error because i was typing urlpatternsin urls.py to urlpattern.
在我来说,我在得到这个错误,因为我打字URL模式在urls.py到 URLPATTERN。
回答by DevB2F
This error also appears if SomeView doesn't exist in views.py and you do this:
如果在 views.py 中不存在 SomeView 并且您执行以下操作,也会出现此错误:
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
因此,请确保您在 urls.py 中使用的所有视图都存在于 views.py 中