【问题标题】:If you see valid patterns in the file then the issue is probably caused by a circular import.(its not a typo but caused by circular import)如果您在文件中看到有效模式,则问题可能是由循环导入引起的。(它不是错字,而是由循环导入引起的)
【发布时间】:2021-06-01 09:04:13
【问题描述】:

这很不寻常。代码运行良好,但突然出现此错误:

     Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\HP\Desktop\Ecommerce\env\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns  
    iter(patterns)
TypeError: 'module' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\hp\appdata\local\programs\python\python36\lib\threading.py", line 916, in _bootstrap_inner       
    self.run()
  File "c:\users\hp\appdata\local\programs\python\python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\HP\Desktop\Ecommerce\env\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper      
    fn(*args, **kwargs)
  File "C:\Users\HP\Desktop\Ecommerce\env\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\HP\Desktop\Ecommerce\env\lib\site-packages\django\core\management\base.py", line 395, in check   
    include_deployment_checks=include_deployment_checks, 
  File "C:\Users\HP\Desktop\Ecommerce\env\lib\site-packages\django\core\management\base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\HP\Desktop\Ecommerce\env\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\HP\Desktop\Ecommerce\env\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)      
  File "C:\Users\HP\Desktop\Ecommerce\env\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', []) 
  File "C:\Users\HP\Desktop\Ecommerce\env\lib\site-packages\django\utils\functional.py", line 48, in __get__      
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\HP\Desktop\Ecommerce\env\lib\site-packages\django\urls\resolvers.py", line 596, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf 'backend.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.

我的后端.urls

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('base.urls')),
]

我的 base.url:

from django.urls import path
from . import views

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

我的基地/views.py

from django.shortcuts import render
from django.http import JsonResponse
from .products import products

# Create your views here.


def getRoutes(request):
    routes = [
        '/api/products/',
        '/api/products/create/',

        '/api/products/upload',

        '/api/products/<id>/reviews/',

        '/api/products/top/',
        '/api/products/<id>/',

        '/api/products/delete/<id>',
        '/api/products/<update>/<id>/',
    ]
    return JsonResponse(routes, safe=False)

我使用 django 很长时间了,现在从来没有见过这个错误,确切的代码运行良好,但是当我在视图中定义第二个 func 时,发生了错误,我删除了那个 func 但错误没有出现。我已经尝试了所有答案,请帮助

【问题讨论】:

  • 请发布完整的回溯。
  • 嗨威廉我已经发布了回溯,请检查。我认为这是 IDE 问题

标签: django django-views django-urls


【解决方案1】:

对我来说,我和你有同样的问题。

我有一个 views.py 文件,我尝试在其中导入所有正确的表单。

它给了我以下错误:

线程 django-main-thread 中的异常:回溯(最后一次调用):文件 “C:\Python36\lib\site-packages\django\urls\resolvers.py”,第 600 行,在 url_patterns iter(patterns) TypeError: 'module' object is not iterable

上述异常是以下异常的直接原因:

Traceback(最近一次调用最后一次):文件 “C:\Python36\lib\threading.py”,第 916 行,在 _bootstrap_inner 中 self.run() 文件“C:\Python36\lib\threading.py”,第 864 行,运行中 self._target(*self._args, **self._kwargs) 文件“C:\Python36\lib\site-packages\django\utils\autoreload.py”,第 64 行, 在包装中 fn(*args, **kwargs) 文件 "C:\Python36\lib\site-packages\django\core\management\commands\runserver.py", 第 118 行,inner_run self.check(display_num_errors=True) 文件“C:\Python36\lib\site-packages\django\core\management\base.py”,行 423,检查中 数据库=数据库,文件“C:\Python36\lib\site-packages\django\core\checks\registry.py”,行 76,在 run_checks new_errors = check(app_configs=app_configs, databases=databases) 文件“C:\Python36\lib\site-packages\django\core\checks\urls.py”,行 13、在check_url_config中 返回 check_resolver(resolver) 文件“C:\Python36\lib\site-packages\django\core\checks\urls.py”,第 23 行, 在 check_resolver 中 返回 check_method() 文件“C:\Python36\lib\site-packages\django\urls\resolvers.py”,第 412 行,在 查看 对于 self.url_patterns 中的模式:文件“C:\Python36\lib\site-packages\django\utils\functional.py”,第 48 行, 在得到 res = instance.dict[self.name] = self.func(instance) 文件“C:\Python36\lib\site-packages\django\urls\resolvers.py”,第 607 行,在 url_patterns raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e django.core.exceptions.ImproperlyConfigured: 包含的 URLconf 'projectWS.urls' 中似乎没有任何模式。 如果您在文件中看到有效模式,那么问题可能是 由循环导入引起的。

urlpatterns 等中的所有内容都是正确的......所以我不明白为什么会发生这种情况。

最后错误是因为在我的views.py中,我放了

from forms import ConversionForm

而不是

from .forms import ConversionForm

所以,可能是导入的问题

【讨论】:

    猜你喜欢
    • 2022-06-12
    • 2021-08-06
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多