2017-06-21 34 views
0

我用戶的Django創建一個名爲testdj項目,以及在它我創建了一個名爲APP TestModel,看到我的樹:導入錯誤:沒有模塊名爲TestModel時使用manage.py到RUNSERVER

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', 
] 
+0

您是否已將'TestModel'添加到'installedApps'? – user3764893

+0

@ user3764893是的,我有。 – aircraft

回答

1

兄弟兄弟兄弟,爲什麼你的arent後的任何教程?你搞砸了

應用程序文件夾和主testdj文件夾會在那裏,你必須manage.py

此時已經插入了testdj文件夾中settings.py是裏面的testmodel,

所以把它併發生在地方manage.py的,整個文件夾,

然後運行

python manage.py runserver 
+0

The turorial does not clear this point that I swear,and I'm it it it:http://www.runoob.com/django/django-model.html – aircraft

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', 
] 

而且你可以添加「testdj」,在安裝的應用程序,如果你想添加views.py文件中設置水平。 請記住,您的應用程序是一個python包,應用程序中的每個文件都是一個python模塊,這就是django使用它們的方式。

相關問題