【问题标题】:STATIC_ROOT setting in DjangoDjango 中的 STATIC_ROOT 设置
【发布时间】:2018-03-05 17:30:13
【问题描述】:

我正在学习 Django 并构建了一个示例 Django 应用程序。它在我的计算机上运行良好,但我无法将其部署到 Heroku。 我的根目录结构如下:

我的settings.py 文件对静态文件有如下设置:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

完整的 settings.py 文件如下:

    """
Django settings for firstdjango project.

Generated by 'django-admin startproject' using Django 1.8.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

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


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'j8s(6fw61+cx_o=g!9a(vs!wbj0&f!7u_lw$(eap5d4li@!b4('

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'inventory',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'firstdjango.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['firstdjango/templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'firstdjango.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

import dj_database_url
db_from_env = dj_database_url.config(conn_max_age = 500)
DATABASES['default'].update(db_from_env)

# STATIC_URL = '/static/'
# STATICFILES_DIRS = [
#    os.path.join(BASE_DIR, "static")
# ]
# STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'    

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

当我尝试部署到 Heroku 时,我收到以下错误消息:

配置不当:您使用的是 staticfiles 应用程序而没有将 STATIC_ROOT 设置设置为文件系统路径

【问题讨论】:

  • @user1787331 没关系,但我想知道在 Django 中设置静态文件的正确方法以及我缺少什么。
  • 我将评论移至答案。
  • 请显示您的设置文件的其余部分。
  • @DanielRoseman 我现在在问题中包含了完整的 settings.py 文件。

标签: python django heroku


【解决方案1】:

这里是 django 的静态根目录的推荐设置 -> heroku 项目

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/    

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'    

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, 'static'),
]

我建议仅使用此 https://github.com/heroku/heroku-django-template 作为起点,然后将您的应用程序导入该项目。由于(截至目前)Heroku 建议使用以下软件包

Gunicorn 
WhiteNoise
dj-database-url

git 项目将为您提供“静态文件、数据库设置、Gunicorn 等的生产就绪配置”。换句话说,它将为您提供将 Django 部署到 Heroku 的正确配置。

【讨论】:

    猜你喜欢
    • 2016-02-24
    • 2018-08-01
    • 1970-01-01
    • 2021-07-31
    • 2012-08-25
    • 1970-01-01
    • 2012-06-03
    • 2016-08-14
    • 2020-10-02
    相关资源
    最近更新 更多