django - 使用 Heroku 的 Django 部署中缺少静态文件 list 条目

标签 django heroku django-staticfiles static-files collectstatic

我正在尝试将我的网络应用程序部署到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 entry 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'

最佳答案

尝试注释该行 'django.contrib.staticfiles', 在 INSTALLED_APPS 内

关于django - 使用 Heroku 的 Django 部署中缺少静态文件 list 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45270394/

相关文章:

python - 使用 while 循环运行 django 微服务?

django admin 在修改 obj 时将字段设置为只读,但在添加新 obj 时需要

heroku - 自定义 Heroku 构建包中的配置变量何时何地在环境中可用?

django - 如何使用 django-cumulus 提供静态文件?

Django Debug=False 白噪声不起作用

mysql - 我无法在包含大量文本的 Django 模型字段上使用索引。我正在使用MySQL。我应该怎么办?

Django REST Serializer - 部分更新仍然更新完整记录

django - 如何在托管在 Heroku 上的 Django 项目中使用 Werkzeug 交互式回溯调试器?

heroku - Cloud IDE 无法连接到 heroku

django - Django Whitenoise缺点