我在项目后端使用django 1.10.5和mongodb。我的项目结构如下 -
project_name
|-- applications
| |-- app1
| | |-- admin.py
| | |-- upload.py
| | |-- __init__.py
| | |-- migrations
| | | `-- __init__.py
| | |-- models.py
| | |-- services.py
| | |-- urls.py
| | |-- views.py
| |-- __init__.py
|-- __init__.py
|-- manage.py
|-- project_name_config
|-- __init__.py
|-- settings
| |-- dev_settings.py
| |-- __init__.py
| |-- prod_settings.py
| |-- settings.py
|-- urls.py
|`-- wsgi.py
我正在使用虚拟环境。我在目录应用程序下使用manage.py创建了应用程序。 根据django教程,当我在settings.py中的INSTALLED_APPS列表下添加app_name时,为 -
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'applications.app1',
]
它给出了错误 -
(virtEnv):~$python manage.py runserver
Unhandled exception in thread started by <function wrapper at 0x7fe94e554848>
Traceback (most recent call last):File "/home/dir_project/virtEnv/local/lib/python2.7/site- packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/dir_project/virtEnv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "/home/dir_project/virtEnv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/dir_project/virtEnv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/dir_project/virtEnv/local/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/dir_project/virtEnv/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/dir_project/virtEnv/local/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named app1
但是当我从settings.py我的应用程序中移除app_name时,视图等一切正常。
考虑到未来范围,是否必须在INSTALLED_APPS列表下的settings.py中添加app_name。
提前致谢。
答案 0 :(得分:1)
为了让您的模型在数据库中创建为表格,或者如果我们想要编写任何自定义模板标签并使它们起作用,我们必须将app_name保存在INSTALLED_APPS中。由于您的实际应用程序app1位于应用程序文件夹中,请将applications.app1
保留在INSTALLED_APPS
而不仅仅是app1