首页 > 解决方案 > Heroku 不收集我的静态文件

问题描述

对不起我的英语不好,但是我的国家没有很多python工程师,所以在这里问一下,希望你能帮助我。

我在heroku上部署了我用Django制作的应用程序,这很好。那时,所有静态文件都已加载。

但是,当我引入 cloudinary(heroku 的附加组件)在我的主页上发布图像时,我的应用程序变得无法像这样加载我的静态文件。

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。如果有人知道任何提示,请告诉我。

设置.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)

标签: pythondjangoheroku

解决方案


推荐阅读