【发布时间】:2021-04-15 01:41:49
【问题描述】:
我在 django 项目 (leadmanager) leads, frontend, accounts 中有 3 个应用程序
如果我在leadmanager.urls 中不包含accounts.urls(来自accounts 应用程序),一切正常,但是一旦包含accounts.urls,我就会收到以下错误(所有这些应用程序都在settings.py 中注册):
$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\urls\resolvers.py", line 591, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Danial Ahmed\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\Danial Ahmed\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\management\base.py", line 396, in check
databases=databases,
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\checks\registry.py", line 70,
in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Danial Ahmed\Desktop\Learning\django-react\venv\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf 'leadmanager.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.
leadmanager.url(主):
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('frontend.urls')),
path('',include('leads.urls')),
path('',include('accounts.urls')),
]
lead.urls:
from rest_framework import routers
from .api import LeadViewSet
router = routers.DefaultRouter()
router.register('api/leads', LeadViewSet, 'leads')
app_name = 'leads'
urlpatterns = router.urls
accounts.urls:
from django.urls import path, include
from .api import ReigsterAPI
from knox import views as knox_views
app_name = 'accounts'
urlpatterns = [
path('api/auth', include('knox.urls')),
path('api/auth/register', RegisterAPI.as_view()),
]
前端.urls:
from django.urls import path
from . import views
urlpatterns = [
path('',views.index),
]
【问题讨论】:
-
如果在您将
accounts.url添加到基本 URL 时发生这种情况,您是否尝试过从accounts.urls中删除这两个路径?或者用简单的TemplateView替换两者? -
@FlipperPA 是的,如果只是将 path('auth/', TemplateView.as_view(template_name='index.html')) 放在 url 模式中,它就可以正常工作。
-
所以让我们找出是
path('api/auth', include('knox.urls')),还是path('api/auth/register', RegisterAPI.as_view()),导致了问题 - 尝试单独注释掉每个问题,看看哪个会引发错误?如果是knox.urls,那么我们将不得不深入该应用程序的urls.py以找到罪魁祸首。 :) -
@FlipperPA 尝试过,但给出了同样的错误
-
@FlipperPA 搞定了!
标签: django django-rest-framework django-urls