Django:ValueError:“ jquery.twbsPaginationAlt.min.js”缺少静态文件清单条目

时间:2020-07-28 14:23:33

标签: python django heroku django-staticfiles whitenoise

在标记为重复项之前,请注意,我已经检查了所有类似的问题,但并没有解决我的问题。我已经将我的Django应用程序部署在Heroku中的一个模板中,该模板引用了一个min.js文件,它引发了该错误。

我的基本目录结构是这样:

myapp
    mysite
    dictionary
    static
        jquery.twbsPaginationAlt.min.js
    staticfiles

settings.py:

STATIC_URL = '/static/'
# STATIC_ROOT = [os.path.join(BASE_DIR, 'staticfiles'), os.path.join(BASE_DIR, 'static/fonts')]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
if DEBUG:
    STATICFILES_DIRS = [os.path.join(BASE_DIR, 'mysite/static'), os.path.join(BASE_DIR, 'static/')]

import django_heroku
# handles staticfiles, database url, and secret key, can be overwritten as needed
django_heroku.settings(locals(), secret_key=False, logging=False)

因此,最初,如果STATICFILES_DIRS具有任何值,则在heroku上运行collectstatic时,我将得到一个FileNotFoundError: [Errno 2] No such file or directory: '/tmp/build_6f3eb879/mysite/static'错误。但是,如果未设置该字体,则将无法正确加载我在开发环境中拥有的字体(我在基本级静态文件夹中也有字体)。

这是我在问题模板中引用js文件的方式,在dev上工作正常:

    <script src='{% static 'jquery.twbsPaginationAlt.min.js' %}'></script>

为方便起见,这是django-heroku在静态文件方面的作用:

        logger.info('Applying Heroku Staticfiles configuration to Django settings.')

        config['STATIC_ROOT'] = os.path.join(config['BASE_DIR'], 'staticfiles')
        config['STATIC_URL'] = '/static/'

        # Ensure STATIC_ROOT exists.
        os.makedirs(config['STATIC_ROOT'], exist_ok=True)

        # Insert Whitenoise Middleware.
        try:
            config['MIDDLEWARE_CLASSES'] = tuple(['whitenoise.middleware.WhiteNoiseMiddleware'] + list(config['MIDDLEWARE_CLASSES']))
        except KeyError:
            config['MIDDLEWARE'] = tuple(['whitenoise.middleware.WhiteNoiseMiddleware'] + list(config['MIDDLEWARE']))

        # Enable GZip.
        config['STATICFILES_STORAGE'] = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

(来自https://github.com/heroku/django-heroku/blob/master/django_heroku/core.py

编辑: 为了解决这个问题,我不得不停止使用django heroku,将STATICFILES_STORAGE变量更改为whitenoise.storage.CompressedStaticFilesStorage,并通过检查日志以查看GET请求的路径来重新组织一些静态文件。根据我所见的其他stackoverflow帖子,whitenoise似乎还很不完善,它始终会丢失根目录中的“ static /”文件。另外,由于Django很大程度上依赖于模板中的'static'关键字,并且.css文件中不提供此关键字,因此解决将静态文件转换为css文件(例如用于字体)的唯一方法是反复试验不同的路径。

1 个答案:

答案 0 :(得分:0)

这行对我来说很奇怪,可能是问题的根源

>>> from datetime import timedelta
>>> timedelta(minutes=2, seconds=1)
datetime.timedelta(seconds=121)

为什么只在调试模式下定义STATICFILES_DIRS?您还需要生产中定义的那些。