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

Python django/whitenoise存储后端导致错误,python,django,heroku,django-staticfiles,django-storage,Python,Django,Heroku,Django Staticfiles,Django Storage,在调试关闭时在heroku上运行django应用程序时,我遇到了一个500错误。 使用rollbar了解错误发生的原因后,报告如下: ValueError: The file 'media/img 1.jpg' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f795706f550>. 在这篇文章中说,尝试使用django的manifestSt

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

ValueError: The file 'media/img 1.jpg' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f795706f550>.
在这篇文章中说,尝试使用django的manifestStaticFiles存储,如果问题仍然存在,那么问题出在django,而不是whitenoise

以下是我的生产设置:

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'

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

urlpatterns=[
...
]+静态(settings.MEDIA\u URL,document\u root=settings.MEDIA\u root)
媒体文件无法提供服务,因为您在project/URL.py中使用的
static
helper函数仅在启用调试时有效。如果您在生产中以这种方式为用户上传的内容提供服务,则会出现问题

如果您确实确信用户的内容是安全的,您当然可以删除此限制。

def static(前缀,view=service,**kwargs):
...
#如果未处于调试模式或非本地前缀,则无操作
如果不是settings.DEBUG或(前缀和前缀中的“:/”):
返回[]
elif非前缀:
raise配置不当(“不允许使用空静态前缀”)
返回[
url(r'^%s(?P.*)$'%re.escape(前缀为.lstrip('/')),视图,kwargs=kwargs),
]

我认为您正在使用
静态
助手功能提供媒体文件,如下所示:

urlpatterns=[
...
]+静态(settings.MEDIA\u URL,document\u root=settings.MEDIA\u root)
媒体文件无法提供服务,因为您在project/URL.py中使用的
static
helper函数仅在启用调试时有效。如果您在生产中以这种方式为用户上传的内容提供服务,则会出现问题

如果您确实确信用户的内容是安全的,您当然可以删除此限制。

def static(前缀,view=service,**kwargs):
...
#如果未处于调试模式或非本地前缀,则无操作
如果不是settings.DEBUG或(前缀和前缀中的“:/”):
返回[]
elif非前缀:
raise配置不当(“不允许使用空静态前缀”)
返回[
url(r'^%s(?P.*)$'%re.escape(前缀为.lstrip('/')),视图,kwargs=kwargs),
]

David,我们也遇到了同样的问题,目前正试图找到解决方案。如果你找到了,请在这里发帖!看到了吗?你遵守了所有的说明了吗?大卫,我们也遇到了同样的问题,目前正试图找到解决方法。如果你找到了,请在这里发帖!看到了吗?你遵守所有的指示了吗?
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'
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),
    ]