简体   繁体   English

Django 中的 STATIC_ROOT 设置

[英]STATIC_ROOT setting in Django

I am learning Django and have built a sample Django app.我正在学习 Django 并构建了一个示例 Django 应用程序。 It works fine on my computer, but I am having trouble deploying it to Heroku.它在我的计算机上运行良好,但我无法将其部署到 Heroku。 My root directory has the following structure:我的根目录具有以下结构: 第一屏

第二屏

My setttings.py file has the following settings for static files:我的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'

The full settings.py file is following:完整的 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'

When I try to deploy to Heroku, I get the following error message:当我尝试部署到 Heroku 时,我收到以下错误消息:

ImproperlyConfigured:You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path ImproperlyConfigured:您正在使用 staticfiles 应用程序,而没有将 STATIC_ROOT 设置设置为文件系统路径

Here are the recommended settings for static root for a django -> heroku project以下是 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'),
]

I would recommend just using this https://github.com/heroku/heroku-django-template as a starting point and import your apps into that project.我建议只使用这个https://github.com/heroku/heroku-django-template作为起点,然后将您的应用程序导入到该项目中。 Due to the fact (as of now) that Heroku recommends using the following packages由于(截至目前)Heroku 建议使用以下软件包

Gunicorn 
WhiteNoise
dj-database-url

The git project will provide you with "Production-ready configuration for Static Files, Database Settings, Gunicorn, etc." git 项目将为您提供“静态文件、数据库设置、Gunicorn 等的生产就绪配置”。 In other words, it will give you the correct configuration to deploy Django to Heroku.换句话说,它将为您提供将 Django 部署到 Heroku 的正确配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM