部署到 Heroku 时 Django 静态文件未通过

Django static files not coming through when deployed to Heroku

当我部署到 Heroku 时,我的静态文件没有通过。当我在本地 运行 时它工作正常。

这不是 404 错误,在网络选项卡上它似乎根本没有请求静态文件。

我在 Heroku 的网站上遵循了这个解决方案:https://devcenter.heroku.com/articles/django-assets 但仍然无法正常工作。

我查看了其他解决方案并且我已经在使用 WhiteNoise。

这是我的settings.py

中的相关代码
import os
import environ
import django_heroku 


env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False)
)

environ.Env.read_env()


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))


# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = (env('DEBUG_VALUE') == "True")

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    #my own apps
    'corsheaders',
    'rest_framework',
    'paintings',
    'storages',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    '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',
]

CORS_ORIGIN_ALLOW_ALL = True

ROOT_URLCONF = 't_and_b_website.urls'


WSGI_APPLICATION = 't_and_b_website.wsgi.application'


STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'staticfiles')

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'staticfiles'), 
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

#MEDIA_ROOT is for directory for any media that our users upload
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media_cdn')
MEDIA_URL = '/media_cdn/'


CORS_URLS_REGEX = r'^/api.*'
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
    '*',
    'your-domain.com',
    'your-bucket-here.s3-us-west-2.amazonaws.com',
)


AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = env('AWS_STORAGE_BUCKET_NAME')


AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None


DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

这是我的 wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "t_and_b_website.settings")

application = get_wsgi_application()

在我的 Heroku 日志中给我这个警告,所以我不确定它是否获取了我的 Heroku 环境变量:

/app/.heroku/python/lib/python3.6/site-packages/environ/environ.py:630: 
UserWarning: /app/src/t_and_b_website/.env doesn't exist - 
if you're not configuring your environment separately, create one.

非常感谢!

抱歉各位 - 我犯了愚蠢的错误。原来问题是我 gitignore 我自己的静态文件夹。