首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >django/whitenoise存储后端导致错误

django/whitenoise存储后端导致错误
EN

Stack Overflow用户
提问于 2016-06-05 19:18:52
回答 1查看 1.5K关注 0票数 2

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

代码语言:javascript
复制
ValueError: The file 'media/img 1.jpg' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f795706f550>.

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

代码语言:javascript
复制
STATICFILES_STORAGE ='django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

whitenoise troubleshooting 中,它说要尝试使用django的manifestStaticFiles存储,如果问题仍然存在,那么问题出在django而不是whitenoise。

以下是我的生产设置:

代码语言:javascript
复制
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'
EN

回答 1

Stack Overflow用户

发布于 2017-02-08 12:42:32

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

代码语言:javascript
复制
urlpatterns = [
   ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

媒体文件无法提供,因为您在项目/urls.py中使用的static辅助函数仅在DEBUG打开时才起作用。如果你在生产中以这种方式提供用户上传的内容,就会有security concerns

如果你确定你的用户的内容是安全的,你当然可以取消这个限制。

代码语言:javascript
复制
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),
    ]
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37641233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档