django / whitenoise存储后端导致错误

时间:2016-06-05 11:18:52

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

我在调试关闭时在heroku上运行我的django应用程序时遇到500错误。 在使用滚动条来了解为什么错误是满意之后,它报告了以下内容:

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存储,如果问题仍然存在,则问题出在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'

1 个答案:

答案 0 :(得分:0)

我认为您正在使用 var dateData = [ { "label": "20/11/2012", "value": "880000" }, { "label": "20/11/2013", "value": "730000" }, { "label": "20/11/2014", "value": "590000" }, { "label": "20/11/2015", "value": "520000" }, { "label": "20/11/2016", "value": "330000" } ] for (var i = 0; i < dateData.length; i++) { var dateParts = dateData[i].label.split(/(\d{1,2})\/(\d{1,2})\/(\d{4})/); dateData[i].label = dateParts[3]; } FusionCharts.ready(function () { var topStores = new FusionCharts({ type: 'bar2d', renderAt: 'chart-container', width: '400', height: '300', dataFormat: 'json', dataSource: { "chart": { "caption": "Shares", "subCaption": "Last month", "yAxisName": "Sales (In USD)", "numberPrefix": "$", "paletteColors": "#0075c2", "bgColor": "#ffffff", "showBorder": "0", "showCanvasBorder": "0", "usePlotGradientColor": "0", "plotBorderAlpha": "10", "placeValuesInside": "1", "valueFontColor": "#ffffff", "showAxisLines": "1", "axisLineAlpha": "25", "divLineAlpha": "10", "alignCaptionWithCanvas": "0", "showAlternateVGridColor": "0", "captionFontSize": "14", "subcaptionFontSize": "14", "subcaptionFontBold": "0", "toolTipColor": "#ffffff", "toolTipBorderThickness": "0", "toolTipBgColor": "#000000", "toolTipBgAlpha": "80", "toolTipBorderRadius": "2", "toolTipPadding": "5" }, "data": dateData } }) .render(); }); 辅助函数来提供媒体文件,如下所示:

Sub GetRng()
Dim rng1 As Range
On Error Resume Next
Set rng1 = Application.InputBox("pls select range", "Set Range", Selection.Address, , , , , 8)
On Error GoTo 0
If rng1 Is Nothing Then MsgBox "User cancelled", vbCritical
End Sub

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

如果您确定用户的内容是安全的,则可以删除此限制。

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