django - Heroku:图像在一段时间后不显示
问题描述
我有一个托管在 Heroku 上的 Django APP。
我在管理面板中上传了一些图片,所以在我的网站上显示。上传后的第一分钟/小时,它们显示正常。
然而,几个小时后,它们不再显示。我必须在管理面板中再次上传它们才能正确显示。
当在 HTML 中没有图像出现时:
<img class="my_image_medium" src="/media/category/Artboard_1_PNG.png" alt="Stickers">
重新上传图像的 HTML:
<img class="my_image_medium" src="/media/category/Artboard_1_PNG.png" alt="Stickers">
Django 应用程序设置:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '^_67&#r+(c+%pu&n+a%&dmxql^i^_$0f69)mnhf@)zq-rbxe9z'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', 'stickers-gallito-app.herokuapp.com',
'stickersgallito.pe', 'www.stickersgallito.pe']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'shop',
'search_app',
'cart',
'stripe',
'order',
'crispy_forms',
'embed_video',
]
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
'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',
]
ROOT_URLCONF = 'stickers_gallito.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'shop', 'templates/'),
os.path.join(BASE_DIR, 'search_app', 'templates/'),
os.path.join(BASE_DIR, 'cart', 'templates/'),
os.path.join(BASE_DIR, 'order', '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',
'shop.context_processor.menu_links',
'shop.context_processor.has_shop',
# 'cart.context_processor.current_time',
'cart.context_processor.cart_items_counter'
],
},
},
]
WSGI_APPLICATION = 'stickers_gallito.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'd39a83vqn3afdj',
'USER': 'u9qchlpdl8pbao',
'PASSWORD': 'pcf1d926adfd169634371480b03c270d7da7b9033c81210482571f1a8d0ad689b',
'HOST': 'ec2-3-93-140-235.compute-1.amazonaws.com',
'PORT': '5432',
# 'OPTIONS': {
# 'sslmodule': 'required'
# }
}
}
# add this
# import dj_database_url
# db_from_env = dj_database_url.config()
# DATABASES['default'].update(db_from_env)
# Password validation
# https://docs.djangoproject.com/en/2.1/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/2.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
# STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
CRISPY_TEMPLATE_PACK = 'bootstrap4'
### Culqui Settings ###
CULQI_PUBLISHABLE_KEY = 'pk_test_d4d1nYriXvMV0YKk'
CULQI_SECRET_KEY = 'sk_test_sOlYqSB5PDAkwQuZ'
解决方案
您将上传的图像存储在本地文件系统上。你不能在 Heroku 中做到这一点。
Heroku 实例是短暂的,它们之间不共享文件系统。每当您通过扩展部署或启动新实例时,它都不会包含您上传的任何文件。
您必须将它们存放在更永久的地方。S3是一个不错的选择;有很多第三方 Django 文件存储库可以为你做这件事。
推荐阅读
- c - C 程序中的 Segmentation Fault 11 错误,我必须计算输入单词的出现次数
- java - 在 javafx 中构造 Stage 对象会引发未知错误
- networkx - OSMnx 使用 networkx 找到有向图的最短路径
- python - NameError:名称“InputNumber”未定义
- sql - sql dateiff 比较年龄和hiredate
- python - python中的嵌套结构
- ios - iOS Autoshrink 不适用于 UILabel 中的单个单词
- python - 如何在特定时间(以微秒为单位)从带有 cv2 的视频中提取帧?
- jenkins - Jenkins 声明式:根据条件创建全局变量
- python - 将正则表达式模块与熊猫系列一起使用