为我的Django项目适当设置BASE_DIR和PROJECT_DIR以捕获我的STATICFILES_DIRS

时间:2019-04-23 00:42:31

标签: django python-2.7

下面是我在Django 1.11中的项目结构。

repo-directory/
├── addons
|   |__ project1
|       |__ static
|       |__ template
|       |__ model.py
|   |__ project2
|       |__ static
|       |__ template
|       |__ model.py
├── config
│   ├── settings.py
│   └── urls.py
|   └── wsgi.py
└── manage.py

尝试做的是将STATICFILES_DIRS设置为知道我的静态文件夹在哪里。 因此,我在settings.py文件中进行了以下操作,但仍无法正常工作。

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR, 'static'),
    os.path.join(PROJECT_DIR, "static/fonts/"),
    os.path.join(PROJECT_DIR, "static/extras/"),
    os.path.join(PROJECT_DIR, "static/img/"),
    os.path.join(PROJECT_DIR, "static/js/"),
)

任何帮助将不胜感激。

WSGI配置

"""
WSGI config for config project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

application = get_wsgi_application()

1 个答案:

答案 0 :(得分:1)

您无需列出static目录中的目录。试试这个:

STATICFILES_DIRS = [
    os.path.join(PROJECT_DIR, "addons/project1/static"),
    os.path.join(PROJECT_DIR, "addons/project2/static"),
]

但是,如果您的addons.project1中有addons.project2INSTALLED_APPS,则可以执行以下操作而不必在STATICFILES_DIRS中列出任何内容:

STATICFILES_FINDERS = [
    ...,
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    ...,
]

更新:

但是,我建议如下更改您的PROJECT_DIRBASE_DIR

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_DIR = os.path.dirname(PROJECT_DIR)

,然后如果要使用STATICFILES_DIRS,则按照以下方式更新STATICFILES_DIRS

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "addons/project1/static"),
    os.path.join(BASE_DIR, "addons/project2/static"),
]