【问题标题】:If you see valid patterns in the file then the issue is probably caused by a circular import in django如果您在文件中看到有效模式,则问题可能是由 django 中的循环导入引起的
【发布时间】:2022-06-12 17:50:17
【问题描述】:

我收到此错误“如果您在文件中看到有效模式,则问题可能是由循环导入引起的”。我看到了其他堆栈流问题,我知道错误来自views.py,但我似乎无法弄清楚错误在哪里

views.py/myapp

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

# Create your views here.
def index(request):
    return HttpResponse('<h1>Hey,Welcome</h1>')

urls.py/myapp

from django.urls import path
from myapp import views

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

urls.py/myproject

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

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

Performing system checks...

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

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

Traceback (most recent call last):
  File "C:\Users\anaconda3\lib\threading.py", line 973, in _bootstrap_inner
    self.run()
  File "C:\Users\anaconda3\lib\threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\anaconda3\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\anaconda3\lib\site-packages\django\core\management\commands\runserver.py", line 134, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\anaconda3\lib\site-packages\django\core\management\base.py", line 487, in check
    all_issues = checks.run_checks(
  File "C:\Users\anaconda3\lib\site-packages\django\core\checks\registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Users\anaconda3\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\anaconda3\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
    return check_method()
  File "C:\Users\anaconda3\lib\site-packages\django\urls\resolvers.py", line 481, in check
    messages.extend(check_resolver(pattern))
  File "C:\Users\anaconda3\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
    return check_method()
  File "C:\Users\anaconda3\lib\site-packages\django\urls\resolvers.py", line 480, in check
    for pattern in self.url_patterns:
  File "C:\Users\anaconda3\lib\site-packages\django\utils\functional.py", line 49, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\anaconda3\lib\site-packages\django\urls\resolvers.py", line 706, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'myapp.urls' from 'E:\\projects\\django\\myproject\\myapp\\urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.

【问题讨论】:

  • 能否也添加完整的回溯错误正文?
  • @Roham 完成....
  • myapp/urls.pyurlpattern = et.c 而不是urlpatterns = 。投票结束是一个错字。

标签: python django


【解决方案1】:

您能否将urlpattern 中的urlpattern 更改为urlpatterns,看看是否能解决您的问题。

【讨论】:

  • 请阅读How to Answer。属于简单拼写错误的问题应该关闭而不回答。
【解决方案2】:

除了改变这一行的答案有助于消除错误

在我的应用(子应用)->urls.py

更改下一行

从 .导入视图

从 appname 导入视图

【讨论】:

    猜你喜欢
    • 2021-06-01
    • 2021-08-06
    • 2020-07-11
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    相关资源
    最近更新 更多