【问题标题】:python3 manage.py runserver errorpython3 manage.py runserver 错误
【发布时间】:2018-09-17 21:22:18
【问题描述】:

当我尝试在我的 PC 上启动 (python3 manage.py runserver) 我的 django2.0 webapp 时,我收到以下消息:

正在执行系统检查...

.wrapper 在 0x7fc889c36510 启动的线程中未处理的异常> Traceback(最近一次调用最后一次):

文件“/home/neo/.local/lib/python3.5/site-packages/django/urls/resolvers.py”,第 538 行,在 url_patterns 迭代(模式) TypeError: 'module' 对象不可迭代

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次): 包装器中的文件“/home/neo/.local/lib/python3.5/site-packages/django/utils/autoreload.py”,第 225 行 fn(*args, **kwargs)

文件“/home/neo/.local/lib/python3.5/site-packages/django/core/management/commands/runserver.py”,第 120 行,inner_run self.check(display_num_errors=True)

文件“/home/neo/.local/lib/python3.5/site-packages/django/core/management/base.py”,第 364 行,检查 include_deployment_checks=include_deployment_checks,

文件“/home/neo/.local/lib/python3.5/site-packages/django/core/management/base.py”,第 351 行,在 _run_checks 返回 checks.run_checks(**kwargs)

文件“/home/neo/.local/lib/python3.5/site-packages/django/core/checks/registry.py”,第 73 行,在 run_checks new_errors = 检查(app_configs=app_configs)

文件“/home/neo/.local/lib/python3.5/site-packages/django/core/checks/urls.py”,第 13 行,在 check_url_config return check_resolver(解析器)

文件“/home/neo/.local/lib/python3.5/site-packages/django/core/checks/urls.py”,第 23 行,在 check_resolver 返回 check_method()

文件“/home/neo/.local/lib/python3.5/site-packages/django/urls/resolvers.py”,第 398 行,检查 warnings.extend(check_resolver(pattern))

文件“/home/neo/.local/lib/python3.5/site-packages/django/core/checks/urls.py”,第 23 行,在 check_resolver 返回 check_method()

文件“/home/neo/.local/lib/python3.5/site-packages/django/urls/resolvers.py”,第 397 行,检查 对于 self.url_patterns 中的模式:

get 中的文件“/home/neo/.local/lib/python3.5/site-packages/django/utils/functional.py”,第 36 行 res = instance.dict[self.name] = self.func(instance)

文件“/home/neo/.local/lib/python3.5/site-packages/django/urls/resolvers.py”,第 545 行,在 url_patterns 提出不当配置(msg.format(name=self.urlconf_name)) django.core.exceptions.ImproperlyConfigured:包含的 URLconf '' 似乎没有任何模式。如果您在文件中看到有效模式,则问题可能是由循环导入引起的。

我的应用程序代码:

(/django-examples/mysite):

(设置.py)

INSTALLED_APPS = [
'webexample',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

(urls.py)

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

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

(/django-examples/mysite/webexample):

(urls.py)

from django.urls import path
from . import views

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

(views.py)

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

def index(request):
return HttpResponse("<h3>Hello, world!</h3>")

ubuntu 16.04 django 2.0.4 蟒蛇3.5 点 8.1.1

问题的原因是什么?

【问题讨论】:

  • 您发布的代码看起来不错(除了缩进),应该不会导致该错误。

标签: python django python-3.x


【解决方案1】:

The order of INSTALLED_APPS is significant!

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'webexample',
]

【讨论】:

  • 我重新安装了我的操作系统和所有软件。现在我的代码和你的一样了控制台的错误已经消失了,但我仍然无法得到这个“Hello, world”签名:
【解决方案2】:

要将您的应用程序添加到主项目文件夹中的 settings.py 中,您需要编写以下内容:

INSTALLED_APPS = [
    '*AppName*.apps.*Class*',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    ]

AppName 将是您的应用程序的名称,Class 必须从您的 App 文件夹中的 apps.py 获取。 这里的 AppName 似乎是 webexample,它的 apps.py 中唯一的类的名称将替换该类。 我希望这能解决您的问题。

【讨论】:

    【解决方案3】:

    如果你有rest框架或mysql,那么你需要在安装的应用程序中提及它。

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'rest_framework',
        'django_mysql',
    ]
    

    【讨论】:

      【解决方案4】:

      尝试更改 urls.py

      from django.contrib import admin
      from django.urls import include, path
      
      urlpatterns = [
      path('admin/', admin.site.urls),
      path('', include('webexample.urls')),
      ]
      

      【讨论】:

        【解决方案5】:

        项目 urls.py 应该这样更改:

        from django.contrib import admin
        from django.urls import include, path
        
        urlpatterns = [
            path('admin/', admin.site.urls),
            path('', include('webexample.urls')),
        ]
        

        View.py 应该这样修改:

        from django.shortcuts import render
        from django.http import HttpResponse
        
        def index(request):
            return HttpResponse("<h3>Hello, world!</h3>")
        

        【讨论】:

          猜你喜欢
          • 2018-01-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-10
          • 1970-01-01
          • 2021-12-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多