Je crée un projet nommé Django testdj
, et aussi dans ce que j'ai créé une application nommée TestModel
, voir mon arbre:
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
Mais quand je cd
au testdj/
répertoire, je veux exécuter le serveur wsgi:
$ python manage.py runserver
Je reçois l'erreur ci-dessous:
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
Ça dit ImportError: No module named TestModel
dedans.
dans le testdj/testdj/settins.py
, J'ai ajouté le TestModel comme APP:
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"TestModel",
]
Réponses:
1 pour la réponse № 1Bro Bro Bro, pourquoi ne suivez-vous pas un tutoriel? tu as fait n'importe quoi
le dossier de l'application et le dossier principal testdj seront là où vous avez manage.py
,
à ce stade, vous avez inséré le testmodel dans le dossier testdj où settings.py
est ,
alors sortez-le et placez-le à la place de manage.py, de tout le dossier,
puis courir
python manage.py runserver
0 pour la réponse № 2
Ce (https://stackoverflow.com/a/44673465/6563567) fonctionnera mais il n’est pas faux de déplacer votre applicationau niveau settings.py. Il suffit de dire à Django où trouver les applications. Étant donné que vos applications ne sont pas au niveau de manage.py, vos applications installées doivent ressembler à ceci:
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"testdj.TestModel",
]
En outre, vous pouvez ajouter "testdj" dans les applications installéessi vous souhaitez ajouter le fichier views.py dans le niveau de configuration. N'oubliez pas que votre application est un package python et que chaque fichier de l'application est un module python et que c'est ainsi que django les utilise.