缺少静态文件使用Heroku清除Django部署中的条目

时间:2017-07-23 22:20:06

标签: django heroku django-staticfiles static-files collectstatic

我正在尝试将我的webapp部署到heroku。我正在使用Django,以下是我的大多数settings.py文件:

"""
Django settings for blog project on Heroku. For more info, see:
https://github.com/heroku/heroku-django-template

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os
import dj_database_url
import raven

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

try:
    from .local_settings import *
except ImportError:
    pass

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    # Disable Django's own staticfiles handling in favour of WhiteNoise, for
    # greater consistency between gunicorn and `./manage.py runserver`. See:
    # http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
    'blogapp',
    'homeapp',
    'nimfksapp',
    'omniclipapp',

    #Heroku Sentry
    'raven.contrib.django.raven_compat',
]

MIDDLEWARE_CLASSES = [
    '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.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'blog.urls'

WSGI_APPLICATION = 'blog.wsgi.application'

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_ROOT = os.path.join(BASE_DIR, 'live-static', 'static-root')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

MEDIA_URL = "/media/"

MEDIA_ROOT = os.path.join(BASE_DIR, "live-static", "media-root")

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

在本地运行collectstatic时,静态文件可以很好地收集并在网页上提供。当我将应用程序部署到heroku(它自动为我运行collectstatic命令)时,它会抛出“内部服务器错误(500)”。我按照类似SO页面上的建议,通过heroku向应用程序添加了一个哨兵,并得到了更明确的错误消息:

  

ValueError / nimfks / errorMissing staticfiles manifest for ...

我尝试更改(并删除)STATICFILES_STORAGE但未成功。删除它让我传递了错误,但试图显示实际的静态文件(在这种情况下是一个图像)没有工作,即它没有加载。

我还尝试更改静态设置以使用PROJECT_ROOT而不是BASE_DIR以及许多不同的配置,所有配置都无效。

我在这里遗漏了什么吗?我似乎无法在SO上提出的类似问题中找到答案。

修改

答案

尴尬,因为它可能会帮助其他人努力解决这个问题。我的问题是我在我的html文件中使用.PNG而不是.png这是真正的文件扩展名。没有意识到这是区分大小写的。这个问题的真正解决方案基本上只是运行一个能够捕获这些类型错误的哨兵(参见sentry docs)。这是我在settings.py文件中的静态设置的最终版本:

import os
import dj_database_url
import raven

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

    MIDDLEWARE_CLASSES = [
    '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.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

WSGI_APPLICATION = 'blog.wsgi.application'

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.storage.CompressedManifestStaticFilesStorage'

1 个答案:

答案 0 :(得分:-3)

尝试评论该行     的 'django.contrib.staticfiles', INSTALLED_APPS内部