gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:28:18 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com