我有一个规范形式的django项目:
. ├── app1/ ├── app2/ ├── ... ├── project/ └── manage.py
如果已经开始添加的所有其他文件夹都不是应用程序(例如文档),那么它会越来越难以保持整洁,所以我决定这样做:
. ├── modules/ │ ├── __init__.py │ ├── app1/ │ └── app2/ ├── project/ └── manage.py
但是,在我的生活中,我无法弄清楚如何在我的settings.py INSTALLED_APPS中引用这些应用。改变
INSTALLED_APPS = [ ... 'app1.apps.App1Config' --> 'modules.app1.apps.App1Config' 'app2.apps.App2Config' --> 'modules.app2.apps.App1Config' ]
将在./manage.py runserver上产生神秘的回溯(/ path / to / venv /为了简洁)
Traceback (most recent call last): File "/path/to/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper fn(*args, **kwargs) File "/path/to/venv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run autoreload.raise_last_exception() File "/path/to/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception six.reraise(*_exception) File "/path/to/venv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise raise value.with_traceback(tb) File "/path/to/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper fn(*args, **kwargs) File "/path/to/venv/lib/python3.5/site-packages/django/__init__.py", line 27, in setup apps.populate(settings.INSTALLED_APPS) File "/path/to/venv/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate app_config = AppConfig.create(entry) File "/path/to/venv/lib/python3.5/site-packages/django/apps/config.py", line 142, in create app_module = import_module(app_name) File "/path/to/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 986, in _gcd_import File "", line 969, in _find_and_load File "", line 956, in _find_and_load_unlocked ImportError: No module named 'app1'
奇怪的是,如果我从INSTALLED_APPS中删除它们,项目将(几乎没有)起作用。例如,请求被汇集到各自的urls.py,呈现视图等等。
我尝试使用新的干净虚拟环境创建一个空的新项目,只有Django 1.10在Python 3.5上运行,但我无法理解。 (是的,无论哪里有__init__.py
)
我错过了什么?
答案 0 :(得分:2)
除了更新INSTALLED_APPS
之外,您还需要更新AppConfig.name
属性以使用新路径。例如:
class App1Config(AppConfig):
name = 'modules.app1'
...