ImportError:将manage.py用于runserver时没有名为TestModel的模块

时间:2017-06-21 10:09:49

标签: python django module

我用户Django创建了一个名为testdj的项目,并在其中创建了一个名为TestModel的APP,请参阅我的树:

aircraftdeMacBook-Pro:TestPython ldl$ tree testdj/
testdj/
├── db.sqlite3
├── manage.py
├── templates
│   ├── base.html
│   ├── ext-1.html
│   ├── hello.html
│   └── index.html
└── testdj
    ├── TestModel
    │   ├── __init__.py
    │   ├── admin.py
    │   ├── apps.py
    │   ├── migrations
    │   │   └── __init__.py
    │   ├── models.py
    │   ├── tests.py
    │   └── views.py
    ├── __init__.py
    ├── __init__.pyc
    ├── helloworld.py
    ├── settings.py
    ├── settings.pyc
    ├── urls.py
    ├── urls.pyc
    ├── view.py
    ├── view.pyc
    ├── wsgi.py
    └── wsgi.pyc

但当我cdtestdj/目录时,我想运行wsgi服务器:

$ python manage.py runserver

我收到以下错误:

Unhandled exception in thread started by <function wrapper at 0x10b80ede8>
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/core/management/commands/runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/utils/autoreload.py", line 250, in raise_last_exception
    six.reraise(*_exception)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/apps/config.py", line 94, in create
    module = import_module(entry)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named TestModel

它中有ImportError: No module named TestModel

testdj/testdj/settins.py中,我已将TestModel添加为APP:

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

2 个答案:

答案 0 :(得分:1)

Bro bro bro,为什么你不遵循任何教程?你搞砸了

app文件夹和主testdj文件夹将位于form.getRows

的位置

此时您已将testmodel插入manage.py所在的testdj文件夹中,

所以把它拿出来放在manage.py的地方,整个文件夹,

然后运行

settings.py

答案 1 :(得分:0)

此(https://stackoverflow.com/a/44673465/6563567)可以使用,但将应用移至settings.py级别并没有错。 你只需要告诉django在哪里找到应用程序。 由于您的应用不在manage.py级别,因此您安装的应用应如下所示:

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

此外,如果要在设置级别添加views.py文件,可以在已安装的应用中添加&#39; testdj&#39; 请记住,您的应用程序是一个python包,应用程序中的每个文件都是一个python模块,这就是django如何使用它们。