我为我的英语不好而感到抱歉,但是我的国家没有很多python工程师,所以让我在这里问一下,希望您对我有所帮助。
我在heroku上部署了用Django开发的应用程序,这很好。那时,所有静态文件都已加载。
但是,当我引入了heroku的附件cloudinary在我的主页上发布图像时,我的应用就无法加载这样的静态文件。
Counting objects: 42, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (41/41), done.
Writing objects: 100% (42/42), 3.89 KiB | 398.00 KiB/s, done.
Total 42 (delta 32), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing requirements with pip
remote:
remote: -----> $ python manage.py collectstatic --noinput
remote: 0 static files copied to
'/tmp/build_myappservercode/staticfiles'.
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 56.4M
remote: -----> Launching...
remote: Released v31
remote: https://myappname.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done..
To https://git.heroku.com/myappname.git
2ba59f3..56178c8 master -> master
它表示“将0个静态文件复制到〜/ staticfiles”。 当我运行“ heroku run python manage.py findstatic。”时,控制台响应如下
Found '.' here:
/app/static
/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/static
/app/.heroku/python/lib/python3.6/site-packages/cloudinary/static
静态文件已完美加载到本地环境中(127.0.0.1:8000)。 我不知道为什么远程会忽略我的静态文件。我放了settings.py和wsgi.py。如果有人知道任何提示,请告诉我。
settings.py
import os
import cloudinary
import cloudinary_storage
import whitenoise
# 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.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'my_secret_key'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'cloudinary_storage',
'django.contrib.staticfiles',
'homepage',
'cloudinary',
]
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',
]
MIDDLEWARE_CLASSES = [
'whitenoise.middleware.WhiteNoiseMiddleware',
]
ROOT_URLCONF = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.0/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.0/topics/i18n/
LANGUAGE_CODE = 'ja-JP'
TIME_ZONE = 'Asia/Tokyo'
USE_I18N = True
USE_L10N = True
USE_TZ = False
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
# try latter STATIC_ROOT
# when it failed, remove the comment-out bellow and delete latter STATIC_ROOT
# STATIC_ROOT = 'staticfiles'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# STATICFILES_STORAGE =
'whitenoise.storage.CompressedManifestStaticFilesStorage'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
# Add to this list all the locations containing your static files
)
# MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# MEDIA_URL = '/media/'
LOGOUT_REDIRECT_URL = 'top_page'
LOGIN_REDIRECT_URL = 'my_opinion'
import dj_database_url
DATABASES['default'] = dj_database_url.config()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ALLOWED_HOSTS = ['*']
DEBUG = False
try:
from .local_settings import *
except ImportError:
pass
# Cloudinary settings
CLOUDINARY_STORAGE = {
'CLOUD_NAME': 'my_cloud_name',
'API_KEY': 'my_key',
'API_SECRET': 'my_api_secret'
}
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage
我要通过注释删除来删除“ STATICFILES_STORAGE”,因为添加该代码时会出现错误,并且我的推送将被拒绝。
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)