在部署到heroku后无法在Django中加载照片



我用Django做了一个作品集+博客网站。它在本地运行时工作完美,但在我将它部署到Heroku后,访问投资组合将我重定向到500服务器错误。我打开调试模式,当我做同样的事情时,它没有抛出500服务器错误,但是,图片不会加载。这非常令人困惑,非常感谢您的帮助……

settings.py

from pathlib import Path
import os
from dotenv import load_dotenv
load_dotenv()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG_PROPAGATE_EXCEPTIONS = True

ALLOWED_HOSTS = []

# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'projects',
'blog',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
]
ROOT_URLCONF = 'personal_portofolio.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["personal_portofolio/templates/"],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'personal_portofolio.wsgi.application'

# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}

# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR,  'templates'),
)
import django_heroku
django_heroku.settings(locals())

wsgi.py

import os
from django.core.wsgi import get_wsgi_application
from whitenoise import WhiteNoise
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'personal_portofolio.settings')
application = get_wsgi_application()
application = WhiteNoise(application)

项目目录:

C:.
├───blog
│   ├───migrations
│   │   └───__pycache__
│   ├───templates
│   └───__pycache__
├───personal_portofolio
│   ├───templates
│   └───__pycache__
├───projects
│   ├───migrations
│   │   └───__pycache__
│   ├───static
│   │   └───img
│   ├───templates
│   └───__pycache__
└───staticfiles
└───admin
├───css
│   └───vendor
│       └───select2
├───fonts
├───img
│   └───gis
└───js
├───admin
└───vendor
├───jquery
├───select2
│   └───i18n
└───xregexp
编辑:将DEBUG_PROPAGATE_EXCEPTIONS设置为True后,我在Heroku日志ValueError: Missing staticfiles manifest entry for 'staticfiles/project1.png' 中得到此错误

settings.py看起来不错,试着将这段代码添加到project-name/urls.py

from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) # add static file URL to django urlpatterns

如果你想托管媒体文件请注意,Heroku不支持媒体托管,你可能需要将你的Django应用程序连接到amazon s3 bucket. 你可以在这里找到一篇相关的文章。

最新更新