【问题标题】:Django application on Heroku doesn't seem to pick up changes to static filesHeroku 上的 Django 应用程序似乎没有获取对静态文件的更改
【发布时间】:2015-01-15 06:24:21
【问题描述】:

我正在 Heroku 上运行一个 django 应用程序。它能够很好地提供静态文件,当我将 new 静态文件推送到我的存储库时,它们会正确显示,但是当我对现有静态文件(例如CSS 更改)它们似乎没有被采纳。

我的产品将其静态文件存储在我的每个 django 应用程序中名为 static/ 的文件夹中。然后,静态根目录位于名为 static_root 的文件夹中。

当 heroku 执行 python manage.py collectstatic 时,对 CSS 文件的更改似乎不会传播到 static_root(尽管正确的原始 CSS 文件确实存在)。我尝试运行python manage.py collectstatic --clear,然后将更改的文件拉到static_root,但服务器仍然为旧文件提供服务,一段时间后,旧版本的 CSS 文件又回到了@ 987654328@。即使我尝试在旧版本中手动复制更改始终是服务器发送的版本,并且旧版本迟早会以static_root结束。

有人知道为什么会这样吗?

为了证明我的配置是正确的,这里是我的settings.pywsgi.py

settings.py

import os
import dj_database_url


BASE_DIR = os.path.dirname(os.path.dirname(__file__))
ROOT_PATH = os.path.join(os.path.dirname(__file__), '..')

DEBUG = bool(int(os.getenv("DJANGO_DEBUG", 1)))
TEMPLATE_DEBUG = DEBUG

STATIC_ROOT = os.path.join(ROOT_PATH, 'jumpcut', 'static_root')

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(ROOT_PATH, 'jumpcut', 'static'),
    os.path.join(ROOT_PATH, 'viewer', 'static'),
    os.path.join(ROOT_PATH, 'builder', 'static'),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'jumpcut.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'jumpcut.wsgi.application'

TEMPLATE_DIRS = (
    os.path.join(ROOT_PATH, 'builder', 'templates'),
    os.path.join(ROOT_PATH, 'viewer', 'templates'),
    os.path.join(ROOT_PATH, 'jumpcut', 'templates'),
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Our custom apps
    # REDACTED
)

wsgi.py

import os

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

from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())

【问题讨论】:

  • 如何更改现有的静态文件?如果您直接通过 Heroku 上的 CLI 进行更改,而不是推送您的更改,它们将在 dynos 重新启动时消失。见dynos ephemeral-filesystem
  • 我在本地进行更改并通过 git 推送它们,但 static_root 列在.gitignore 下,因此可以解释为什么更改没有通过。目前我尝试通过heroku run python manage.py collectstatic --clear 通过heroku 实例传播文件。这还不够吗?

标签: python django heroku


【解决方案1】:

感谢 dazedconfused 的评论,我找到了解决方案。

问题是 Heroku 的 ephemeral filesystem 正在恢复我的更改。我的解决方案是从我的.gitignore 中删除我的static_root,以便在我推送我的更改时 Heroku 获取新的静态文件。

【讨论】:

    猜你喜欢
    • 2021-04-02
    • 2012-04-15
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 2012-02-21
    • 2015-01-24
    • 2016-02-02
    • 2021-06-03
    相关资源
    最近更新 更多