【问题标题】:django/whitenoise storage backend causes an errordjango/whitenoise 存储后端导致错误
【发布时间】:2016-10-05 02:31:36
【问题描述】:

在关闭调试时在 heroku 上运行我的 django 应用程序时遇到 500 错误。 在使用 rollbar 了解错误发生的原因后,它报告了以下内容:

ValueError: The file 'media/img 1.jpg' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f795706f550>.

我发现它与 STATICFILES_STORAGE 设置有关,通过删除它并使用默认的 django STATICFILES_STORAGE ='django.contrib.staticfiles.storage.StaticFilesStorage' 设置,它的工作原理。但是这三个中的任何一个都不起作用,并且都会导致相同的错误:

STATICFILES_STORAGE ='django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

whitenoise troubleshooting 中说尝试使用 django 的 manifestStaticFiles Storage,如果问题仍然存在,则问题出在 django 而不是白噪声。

这些是我的生产设置:

from django.conf import settings

import os

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

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',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'rollbar.contrib.django.middleware.RollbarNotifierMiddleware',
)


DEBUG = False

# Email debugging configuration

ADMINS = (
    ('david', 'davidsidf@gmail.com'),
)

EMAIL_USE_TLS = True

EMAIL_HOST = 'smtp.gmail.com'

EMAIL_HOST_USER = 'davidsidf@gmail.com'

EMAIL_HOST_PASSWORD = '*******'

EMAIL_PORT = 587


# Honor the 'X-Forwarded-Proto' header for request.is_secure()

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['evening-garden-60868.herokuapp.com']

ROLLBAR = {
    'access_token': '*******************',
    'environment': 'development' if DEBUG else 'production',
    'branch': 'master',
    'root': '/absolute/path/to/code/root',
}

STATICFILES_DIRS = (
    os.path.join(BASE_DIR,"studio", "static"),
 )

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

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

【问题讨论】:

标签: python django heroku django-staticfiles django-storage


【解决方案1】:

我认为您正在使用 static 辅助函数提供媒体文件,如下所示:

urlpatterns = [
   ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

媒体文件无法提供服务,因为您在 project/urls.py 中使用的 static 帮助函数仅在 DEBUG 开启时才有效。如果您在生产中以这种方式提供用户上传的内容,则有security concerns

如果您确实确定您的用户的内容是安全的,您可以取消此限制。

def static(prefix, view=serve, **kwargs):
    ...
    # No-op if not in debug mode or an non-local prefix
    if not settings.DEBUG or (prefix and '://' in prefix):
        return []
    elif not prefix:
        raise ImproperlyConfigured("Empty static prefix not permitted")
    return [
        url(r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs),
    ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 2016-01-11
    • 2014-02-02
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多