找不到 heroku 本地静态

heroku local static not found

'heroku local' 找不到静态文件,但 'python manage.py runserver' 找到静态文件没有问题。你能帮我解决这个问题吗?

settings.py 读作:

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

我运行收集static并将静态文件复制到STATIC_ROOT:

python manage.py collectstatic

manage.py 运行server' 找到静态文件没有问题:

python manage.py runserver

同时 'heroku local' returns 警告 'not found':

11:52:04 AM web.1 |  [19/May/2016 10:52:04] WARNING [django.request:170] Not Found: /static/admin/css/base.css
11:52:04 AM web.1 |  [19/May/2016 10:52:04] WARNING [django.request:170] Not Found: /static/admin/css/base.css
11:52:04 AM web.1 |  [19/May/2016 10:52:04] WARNING [django.request:170] Not Found: /static/admin/css/login.css
11:52:04 AM web.1 |  [19/May/2016 10:52:04] WARNING [django.request:170] Not Found: /static/admin/css/login.css

不确定这里发生了什么。 'heroku local' 和 'manage.py runserver' 都应该看到静态文件。

注意: 我正在使用 Django==1.8.2 并且 settings.py 包含:

import os
import dj_database_url

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

SECRET_KEY = 'secretkey'

DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = (
    # 'django.contrib.sites',
    'django.contrib.admin',
    'registration',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'dbbackup',
    'listable',
    'rest_framework',
    'bootstrap3',
    'django_crontab',
)

ACCOUNT_ACTIVATION_DAYS = 7 # One-week activation window; you may, of course, use a different value.
REGISTRATION_AUTO_LOGIN = True # Automatically log the user in.

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'track.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, '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',
            ],
        },
    },
]

WSGI_APPLICATION = 'track.wsgi.application'

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

FIXTURE_DIRS = (
    os.path.join(BASE_DIR, 'fixtures'),
)

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media/'

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ],
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework_xml.parsers.XMLParser',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework_xml.renderers.XMLRenderer',
    ),
}

DATABASES = {'default' : dj_database_url.config(default='postgres://testuser:testpw@localhost:5432/testdb')}

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

BOOTSTRAP3 = {
    'include_jquery': True,
}

我假设这些管理文件来自 Django 管理应用程序。在没有看到您的项目的情况下,我对您接下来应该在哪里进行故障排除的最佳猜测是:

1) 检查 django.contrib.admin 是否在您的 INSTALLED_APPS.

2) 您是否正在更改 DEFAULT_FILE_STORAGE 设置?这可能会干扰 STATICFILES_FINDERS

3) 检查所有 static files settings,如果您要覆盖其中任何一个,请确保没有警告。

如果您 post 整个 settings.py 文件,我会更好地调试您的问题。

适合我的解决方案(24 小时后):

1) 安装最新的稳定版本的 whitenoise(我是从一个旧的例子开始工作的,它需要 whitenoise==2.0.6)

2) 确保将白噪声添加到 MIDDLEWARE_CLASSES:

MIDDLEWARE_CLASSES = (
    'whitenoise.middleware.WhiteNoiseMiddleware',
)

注意:使用whitenoise==2.0.6时在中间件class中加入'whitenoise.middleware.WhiteNoiseMiddleware'会报错。您将需要升级到最新的稳定版本(目前whitenoise==3.1)