在 heroku 上使用 whitenoise 的 Django 静态文件
Django static files using whitenoise on heroku
我正在尝试使用 Heroku's documentation to serve static files on my local machine and in production. However, whenever I run my app with debug=True
everything works as expected; static files are retrieved and the app displays as intended. However, whenever I change debug=False
I get a Server Error (500)
. As far as I can tell it all boils down to my STATICFILE_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
. When I comment that out, my app will run with debug=False
, but with no styling due to the lack of static files. I have gone through the whitenoise documentation, and heroku's,但我不知道发生了什么,除了 STATICFILE_STORAGE
是问题所在。 whitenoise 不适合生产吗?我必须在生产中使用 CDN 吗?这是一个小型应用程序,所以我希望不必使用 CDN,但如果有必要的话。
settings.py(我只包含了 settings.py 中我认为相关的部分。如果您需要全部内容,请告诉我。)
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
MIDDLEWARE = [
'django_hosts.middleware.HostsRequestMiddleware',
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
.......
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/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'),
)
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MySite.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
日志
2017-04-14T08:40:06.800390+00:00 heroku[router]: at=info method=GET path="/" host=wgsite.herokuapp.com request_id=6bf564f7-efba-42fb-b8b1-970a02a5283a fwd="5.51.58.217" dyno=web.1 connect=0ms service=11ms status=302 bytes=223 protocol=https
2017-04-14T08:40:06.997956+00:00 heroku[router]: at=info method=GET path="/login" host=wgsite.herokuapp.com request_id=4ce4486e-7e06-4fcd-a562-88ec3ffd8fa9 fwd="5.51.58.217" dyno=web.1 connect=0ms service=6ms status=302 bytes=243 protocol=https
在此先感谢您的帮助!
更新:
所以我不得不 运行 heroku config:unset DISABLE_COLLECTSTATIC
让 heroku 自动收集静态信息。现在它向我抛出这些错误:
remote: ! Error while running '$ python manage.py collectstatic --noinput'.
remote: See traceback above for details.
这很奇怪,因为我可以 运行 python manage.py 在本地成功收集静态...
更新#2
我在本地 运行 collectstatic
并创建了一个名为 "staticfiles" 的目录,其中包含我在内部按应用程序组织的所有静态文件。我推送到 heroku,现在我的网站打开时所有静态文件都在 debug=False
下运行。我仍然无法让 heroku 在不出错的情况下自动收集静态信息。
事实证明,我的设置目录与这些问题有很大关系。在我的设置目录中,我有一个本地设置文件和一个生产设置文件。结果,我不得不调整 BASE_DIR
和 PROJECT_ROOT
目录。我添加了 PROJECT_ROOT2
目录,以便让 heroku 找到我的本地静态文件。在那之后,我实际上不得不将一个空白 css 文件放入所需的 settings/static 目录,因为 git 无法读取空目录。在所有这些之后,我终于能够在本地和 heroku 上将 collectstatic 设置为 运行。这是我更新的settings.py:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
PROJECT_ROOT2 = os.path.dirname(os.path.abspath(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['www.site.co', 'site', 'wgsite.herokuapp.com']
# Application definition
INSTALLED_APPS = [
# Django Apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Third Party Apps
'crispy_forms',
'django.contrib.humanize',
'django_hosts',
# My Apps
'argent',
'home',
'accounts',
]
MIDDLEWARE = [
'django_hosts.middleware.HostsRequestMiddleware',
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'MySite.middleware.LoginRequiredMiddleware',
'django_hosts.middleware.HostsResponseMiddleware',
]
LOGIN_URL = '/login'
LOGIN_EXEMPT_URLS = [
'/logout',
'/register',
]
ROOT_URLCONF = 'MySite.urls'
ROOT_HOSTCONF = 'MySite.hosts'
DEFAULT_HOST = 'www'
DEFAULT_REDIRECT_URL = 'http://www.site.co'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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 = 'MySite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'CET'
USE_I18N = True
USE_L10N = True
USE_TZ = True
CRISPY_TEMPLATE_PACK = 'bootstrap3'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/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_ROOT2, 'static'),
)
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
我正在尝试使用 Heroku's documentation to serve static files on my local machine and in production. However, whenever I run my app with debug=True
everything works as expected; static files are retrieved and the app displays as intended. However, whenever I change debug=False
I get a Server Error (500)
. As far as I can tell it all boils down to my STATICFILE_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
. When I comment that out, my app will run with debug=False
, but with no styling due to the lack of static files. I have gone through the whitenoise documentation, and heroku's,但我不知道发生了什么,除了 STATICFILE_STORAGE
是问题所在。 whitenoise 不适合生产吗?我必须在生产中使用 CDN 吗?这是一个小型应用程序,所以我希望不必使用 CDN,但如果有必要的话。
settings.py(我只包含了 settings.py 中我认为相关的部分。如果您需要全部内容,请告诉我。)
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
MIDDLEWARE = [
'django_hosts.middleware.HostsRequestMiddleware',
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
.......
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/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'),
)
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MySite.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
日志
2017-04-14T08:40:06.800390+00:00 heroku[router]: at=info method=GET path="/" host=wgsite.herokuapp.com request_id=6bf564f7-efba-42fb-b8b1-970a02a5283a fwd="5.51.58.217" dyno=web.1 connect=0ms service=11ms status=302 bytes=223 protocol=https
2017-04-14T08:40:06.997956+00:00 heroku[router]: at=info method=GET path="/login" host=wgsite.herokuapp.com request_id=4ce4486e-7e06-4fcd-a562-88ec3ffd8fa9 fwd="5.51.58.217" dyno=web.1 connect=0ms service=6ms status=302 bytes=243 protocol=https
在此先感谢您的帮助!
更新:
所以我不得不 运行 heroku config:unset DISABLE_COLLECTSTATIC
让 heroku 自动收集静态信息。现在它向我抛出这些错误:
remote: ! Error while running '$ python manage.py collectstatic --noinput'.
remote: See traceback above for details.
这很奇怪,因为我可以 运行 python manage.py 在本地成功收集静态...
更新#2
我在本地 运行 collectstatic
并创建了一个名为 "staticfiles" 的目录,其中包含我在内部按应用程序组织的所有静态文件。我推送到 heroku,现在我的网站打开时所有静态文件都在 debug=False
下运行。我仍然无法让 heroku 在不出错的情况下自动收集静态信息。
事实证明,我的设置目录与这些问题有很大关系。在我的设置目录中,我有一个本地设置文件和一个生产设置文件。结果,我不得不调整 BASE_DIR
和 PROJECT_ROOT
目录。我添加了 PROJECT_ROOT2
目录,以便让 heroku 找到我的本地静态文件。在那之后,我实际上不得不将一个空白 css 文件放入所需的 settings/static 目录,因为 git 无法读取空目录。在所有这些之后,我终于能够在本地和 heroku 上将 collectstatic 设置为 运行。这是我更新的settings.py:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
PROJECT_ROOT2 = os.path.dirname(os.path.abspath(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['www.site.co', 'site', 'wgsite.herokuapp.com']
# Application definition
INSTALLED_APPS = [
# Django Apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Third Party Apps
'crispy_forms',
'django.contrib.humanize',
'django_hosts',
# My Apps
'argent',
'home',
'accounts',
]
MIDDLEWARE = [
'django_hosts.middleware.HostsRequestMiddleware',
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'MySite.middleware.LoginRequiredMiddleware',
'django_hosts.middleware.HostsResponseMiddleware',
]
LOGIN_URL = '/login'
LOGIN_EXEMPT_URLS = [
'/logout',
'/register',
]
ROOT_URLCONF = 'MySite.urls'
ROOT_HOSTCONF = 'MySite.hosts'
DEFAULT_HOST = 'www'
DEFAULT_REDIRECT_URL = 'http://www.site.co'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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 = 'MySite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'CET'
USE_I18N = True
USE_L10N = True
USE_TZ = True
CRISPY_TEMPLATE_PACK = 'bootstrap3'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/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_ROOT2, 'static'),
)
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'