django 中的循环导入错误



我组织了我的django结构,有一个视图和urls文件夹。 当在/appfolder/urls/_init_.py中导入django.contrib.auth.views.login时,或者实际上在 urls 或 views 文件夹中的任何位置,如下所示:

from django.contrib.auth.views import login

我一直得到:

导入

错误:无法导入名称登录

我认为这与结构的变化有关。我想将其包含在我的 url 模式中,如下所示:

path('portal/', login, {'template_name': 'portal/index.html'})

如何在不出现循环导入错误的情况下导入登录?

目录结构

appfolder
│   
└─── __init__.py       
└─── admin.py
└─── apps.py   
└─── forms.py
└─── models.py         
│
└───urls
│   │   __init__.py     |
│   │   books_urls.py   |
|   |   movies_urls.py  |
|   |   portal_urls.py  |
│   |_ _ _ _ _ _ _ _ _ _|
|    
└───views
│   │   __init__.py      |
│   │   books_views.py   |
|   |   movies_views.py  |
|   |   portal_views.py  |
│   |_ _ _ _ _ _ _ _ _ _ |

appfolder/urls/_ init _.py

from django.urls import include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.auth.views import login
from .books_urls import *
from .movies_urls import *
from .portal_urls import *
urlpatterns = [
path('accounts/', include('django.contrib.auth.urls')),
path('portal/', login, {'template_name': 'portal/index.html'}),
path('portal/', include('appfolder.urls.portal_urls')),
path('movies/', include('appfolder.urls.movies_urls')),
path('books/', include('appfolder.urls.books_urls')),
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

堆栈跟踪:

> Unhandled exception in thread started by <function
> check_errors.<locals>.wrapper at 0x0000013AEBD7D048> Traceback (most
> recent call last):   File
> "C:UsersUserEnvswebshoplibsite-packagesdjangoutilsautoreload.py",
> line 225, in wrapper
>     fn(*args, **kwargs)   File "C:UsersUserEnvswebshoplibsite-packagesdjangocoremanagementcommandsrunserver.py",
> line 117, in inner_run
>     self.check(display_num_errors=True)   File "C:UsersUserEnvswebshoplibsite-packagesdjangocoremanagementbase.py",
> line 379, in check
>     include_deployment_checks=include_deployment_checks,   File "C:UsersUserEnvswebshoplibsite-packagesdjangocoremanagementbase.py",
> line 366, in _run_checks
>     return checks.run_checks(**kwargs)   File "C:UsersUserEnvswebshoplibsite-packagesdjangocorechecksregistry.py",
> line 71, in run_checks
>     new_errors = check(app_configs=app_configs)   File "C:UsersUserEnvswebshoplibsite-packagesdjangocorechecksurls.py",
> line 40, in check_url_namespaces_unique
>     all_namespaces = _load_all_namespaces(resolver)   File "C:UsersUserEnvswebshoplibsite-packagesdjangocorechecksurls.py",
> line 57, in _load_all_namespaces
>     url_patterns = getattr(resolver, 'url_patterns', [])   File "C:UsersUserEnvswebshoplibsite-packagesdjangoutilsfunctional.py",
> line 37, in __get__
>     res = instance.__dict__[self.name] = self.func(instance)   File "C:UsersUserEnvswebshoplibsite-packagesdjangourlsresolvers.py",
> line 533, in url_patterns
>     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)   File
> "C:UsersUserEnvswebshoplibsite-packagesdjangoutilsfunctional.py",
> line 37, in __get__
>     res = instance.__dict__[self.name] = self.func(instance)   File "C:UsersUserEnvswebshoplibsite-packagesdjangourlsresolvers.py",
> line 526, in urlconf_module
>     return import_module(self.urlconf_name)   File "C:UsersUserEnvswebshoplibimportlib__init__.py", line 126, in
> import_module
>     return _bootstrap._gcd_import(name[level:], package, level)   File "<frozen importlib._bootstrap>", line 994, in _gcd_import   File
> "<frozen importlib._bootstrap>", line 971, in _find_and_load   File
> "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked 
> File "<frozen importlib._bootstrap>", line 665, in _load_unlocked  
> File "<frozen importlib._bootstrap_external>", line 678, in
> exec_module   File "<frozen importlib._bootstrap>", line 219, in
> _call_with_frames_removed   File "C:UsersUserDesktopprojectcompanycomcomurls.py", line 10, in
> <module>
>     path('', include('appfolder.urls')),   File "C:UsersUserEnvswebshoplibsite-packagesdjangourlsconf.py",
> line 34, in include
>     urlconf_module = import_module(urlconf_module)   File "C:UsersUserEnvswebshoplibimportlib__init__.py", line 126, in
> import_module
>     return _bootstrap._gcd_import(name[level:], package, level)   File "<frozen importlib._bootstrap>", line 994, in _gcd_import   File
> "<frozen importlib._bootstrap>", line 971, in _find_and_load   File
> "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked 
> File "<frozen importlib._bootstrap>", line 665, in _load_unlocked  
> File "<frozen importlib._bootstrap_external>", line 678, in
> exec_module   File "<frozen importlib._bootstrap>", line 219, in
> _call_with_frames_removed   File "C:UsersUserDesktopprojectcompanycomappfolderurls__init__.py",
> line 8, in <module>
>     from django.contrib.auth.views import login ImportError: cannot import name 'login'

你遇到的问题是,虽然

from .portal_urls import *

只是从当前文件的目录中导入portal_urls.py找到的所有内容,这种类型的导入

import appfolder.urls.portal_urls

将在加载appfolder/urls/portal_urls.py之前同时加载appfolder/__init__.pyappfolder/urls/__init__.py([*] 请参阅底部的示例(。

这就是为什么在您的appfolder/__init__.py编译器通过此导入的原因

...
from .portal_urls import *

在被这个噎住之前:

urlpatterns = [
...
path('portal/', include('appfolder.urls.portal_urls')),

如果您以正常方式构建应用程序,则可以避免此问题,即

appfolder
│   __init__.py
│   urls.py
│
├───books
│       __init__.py
│       urls.py
│
├───movies
│       __init__.py
│       urls.py
│
└───portal
__init__.py
urls.py

其中所有__init__.py文件均为空,appfolder/urls.py包含

urlpatterns = [
...
path(r'^portal/', include('appfolder.portal.urls')),
path(r'^movies/', include('appfolder.movies.urls')),
path(r'^books/', include('appfolder.books.urls')),

这种结构还可以更轻松地将这些文件夹之一拉出到它自己的模块中(如果它增长(。

[*] 如果将print(__file__)放入每个__init__.py文件中,则可以检查导入的内容:

(dev) go|c:srvtmp> python -c "import appfolder.books.urls"
appfolder__init__.py
appfolderbooks__init__.py

相关内容

  • 没有找到相关文章

最新更新