简体   繁体   English

python3 manage.py runserver 错误

[英]python3 manage.py runserver error

When I try to start (python3 manage.py runserver) my django2.0 webapp on my PC I have this message:当我尝试在我的 PC 上启动 (python3 manage.py runserver) django2.0 webapp 时,我收到以下消息:

Performing system checks...正在执行系统检查...

Unhandled exception in thread started by .wrapper at 0x7fc889c36510> Traceback (most recent call last):由 .wrapper 在 0x7fc889c36510> 处启动的线程中的未处理异常> Traceback(最后一次调用):

File "/home/neo/.local/lib/python3.5/site-packages/django/urls/resolvers.py", line 538, in url_patterns iter(patterns) TypeError: 'module' object is not iterable文件“/home/neo/.local/lib/python3.5/site-packages/django/urls/resolvers.py”,第 538 行,在 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 "/home/neo/.local/lib/python3.5/site-packages/django/utils/autoreload.py", line 225, in wrapper fn(*args, **kwargs)回溯(最近一次调用):文件“/home/neo/.local/lib/python3.5/site-packages/django/utils/autoreload.py”,第 225 行,包装器 fn(*args, **kwargs )

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

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

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

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

File "/home/neo/.local/lib/python3.5/site-packages/django/core/checks/urls.py", line 13, in check_url_config return check_resolver(resolver)文件“/home/neo/.local/lib/python3.5/site-packages/django/core/checks/urls.py”,第 13 行,在 check_url_config 中返回 check_resolver(resolver)

File "/home/neo/.local/lib/python3.5/site-packages/django/core/checks/urls.py", line 23, in check_resolver return check_method()文件“/home/neo/.local/lib/python3.5/site-packages/django/core/checks/urls.py”,第 23 行,在 check_resolver 中 return check_method()

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

File "/home/neo/.local/lib/python3.5/site-packages/django/core/checks/urls.py", line 23, in check_resolver return check_method()文件“/home/neo/.local/lib/python3.5/site-packages/django/core/checks/urls.py”,第 23 行,在 check_resolver 中 return check_method()

File "/home/neo/.local/lib/python3.5/site-packages/django/urls/resolvers.py", line 397, in check for pattern in self.url_patterns:文件“/home/neo/.local/lib/python3.5/site-packages/django/urls/resolvers.py”,第397行,检查self.url_patterns中的模式:

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

File "/home/neo/.local/lib/python3.5/site-packages/django/urls/resolvers.py", line 545, in url_patterns raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) django.core.exceptions.ImproperlyConfigured: The included URLconf '' does not appear to have any patterns in it.文件“/home/neo/.local/lib/python3.5/site-packages/django/urls/resolvers.py”,第 545 行,在 url_patterns 中引发 ImproperlyConfigured(msg.format(name=self.urlconf_name)) django。 core.exceptions.ImproperlyConfigured:包含的 URLconf '' 似乎没有任何模式。 If you see valid patterns in the file then the issue is probably caused by a circular import.如果您在文件中看到有效模式,则问题可能是由循环导入引起的。

my app's code :我的应用程序的代码:

(/django-examples/mysite): (/django-examples/mysite):

(Setting.py) (设置.py)

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

(urls.py) (网址.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): (/django-examples/mysite/webexample):

(urls.py) (网址.py)

from django.urls import path
from . import views

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

(views.py) (视图.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 python 3.5 pip 8.1.1 ubuntu 16.04 django 2.0.4 python 3.5 pip 8.1.1

What can be a reason of problem?什么是问题的原因?

The order of INSTALLED_APPS is significant! INSTALLED_APPS的顺序很重要!

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

    'webexample',
]

To add your apps to settings.py in the main project folder, you need to write the following:要将您的应用程序添加到主项目文件夹中的 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',
    ]

The AppName will be the name if your app, Class must be obtained from apps.py in your App folder. AppName 将是您的应用程序的名称,Class 必须从您的 App 文件夹中的apps.py获取。 Here the AppName seems to be webexample , and the name of the only class in its apps.py will replace the class.这里 AppName 似乎是webexample ,其apps.py 中唯一的类的名称将替换该类。 I hope this will resolve your query.我希望这能解决您的疑问。

If you have rest framework or mysql, then you need to mention it in the installed apps.如果你有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',
]

try changing urls.py尝试更改 urls.py

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

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

Project urls.py should be changed like this:项目 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 should be changed like this: View.py 应该像这样改变:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM