Django:如何告诉 Django 它应该在哪里寻找应用程序?
Django: How to tell Django where it should look for apps?
阅读 Django 1.11 的两勺后,我将项目结构更改为:
myname_project
├── config/
│ ├── settings/
│ │ ├── base.py
│ │ ├── local.py
│ │ ├── staging.py
│ │ ├── prod.py
│ ├── __init__.py
│ ├── urls.py
│ └── wsgi.py
├── docs/
├── myname/
│ ├── accounts/ # App
│ ├── blog/ # App
│ ├── core/ # App
│ ├── media/ # Development only!
│ ├── static/
│ └── templates/
├── .gitignore
├── Makefile
├── README.md
├── manage.py
└── requirements/
我现在的问题是,Django 不再找到该应用程序。这是我的设置文件的外观要点:
import os
from pathlib import Path
from django.core.exceptions import ImproperlyConfigured
def get_env_variable(var_name):
"""Get the environment variable or return exception."""
try:
return os.environ[var_name]
except KeyError:
error_msg = 'Set the {} environment variable'.format(var_name)
raise ImproperlyConfigured(error_msg)
# Build paths inside the project like this: BASE_DIR / 'media'
BASE_DIR = Path(__file__).resolve().parent.parent
PROJECT_ROOT = BASE_DIR.parent / 'myname'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
...
'accounts',
'blog',
'core',
]
...
ROOT_URLCONF = 'config.urls'
...
现在的问题是,当我 运行 python manage.py makemigrations --settings=config.settings.local
我得到错误:ModuleNotFoundError: No module named 'accounts'
.
所以 Django 似乎无法再找到我的应用程序。我如何告诉 Django 应该在根目录中查找应用程序?
我尝试使用 PROJECT_ROOT = BASE_DIR.parent / 'myname'
,但没有用。而且我找不到如何在 Django 文档中设置(伪代码:) APP_DIR
。
将 sys.path.append(os.path.join(BASE_DIR, "myname"))
行添加到 settings.py
:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(BASE_DIR, "myname"))
或使用路径库:
BASE_DIR = Path(__file__).resolve().parent.parent
sys.path.append(str(BASE_DIR / 'myname'))
阅读 Django 1.11 的两勺后,我将项目结构更改为:
myname_project
├── config/
│ ├── settings/
│ │ ├── base.py
│ │ ├── local.py
│ │ ├── staging.py
│ │ ├── prod.py
│ ├── __init__.py
│ ├── urls.py
│ └── wsgi.py
├── docs/
├── myname/
│ ├── accounts/ # App
│ ├── blog/ # App
│ ├── core/ # App
│ ├── media/ # Development only!
│ ├── static/
│ └── templates/
├── .gitignore
├── Makefile
├── README.md
├── manage.py
└── requirements/
我现在的问题是,Django 不再找到该应用程序。这是我的设置文件的外观要点:
import os
from pathlib import Path
from django.core.exceptions import ImproperlyConfigured
def get_env_variable(var_name):
"""Get the environment variable or return exception."""
try:
return os.environ[var_name]
except KeyError:
error_msg = 'Set the {} environment variable'.format(var_name)
raise ImproperlyConfigured(error_msg)
# Build paths inside the project like this: BASE_DIR / 'media'
BASE_DIR = Path(__file__).resolve().parent.parent
PROJECT_ROOT = BASE_DIR.parent / 'myname'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
...
'accounts',
'blog',
'core',
]
...
ROOT_URLCONF = 'config.urls'
...
现在的问题是,当我 运行 python manage.py makemigrations --settings=config.settings.local
我得到错误:ModuleNotFoundError: No module named 'accounts'
.
所以 Django 似乎无法再找到我的应用程序。我如何告诉 Django 应该在根目录中查找应用程序?
我尝试使用 PROJECT_ROOT = BASE_DIR.parent / 'myname'
,但没有用。而且我找不到如何在 Django 文档中设置(伪代码:) APP_DIR
。
将 sys.path.append(os.path.join(BASE_DIR, "myname"))
行添加到 settings.py
:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(BASE_DIR, "myname"))
或使用路径库:
BASE_DIR = Path(__file__).resolve().parent.parent
sys.path.append(str(BASE_DIR / 'myname'))