Django:ValueError:缺少“jquery.twbspaginational.min.js”的staticfiles清单条目

2024-06-02 05:40:29 发布

您现在位置:Python中文网/ 问答频道 /正文

在标记为重复之前,请注意,我已经检查了所有已被问到的类似问题,但它并没有解决我的问题。我已经在Heroku上部署了我的Django应用程序,在一个模板中,我引用了一个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\u 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_存储变量更改为whitenoise.STORAGE.CompressedStaticFilesStorage,并通过检查日志查看GET请求的路径来重新组织我保存一些静态文件的位置。从我所看到的其他stackoverflow帖子来看,whitenoise似乎很不完美,它总是遗漏根目录中的“static/”文件。此外,由于Django在模板中非常依赖“static”关键字,而这在.css文件中不可用,因此解决将静态文件转换为css文件(例如字体)的唯一方法是反复测试不同路径


Tags: 文件pathdjangoconfigbaseherokuosdir
1条回答
网友
1楼 · 发布于 2024-06-02 05:40:29

这一行在我看来很奇怪,可能是问题的根源:

if DEBUG:
    STATICFILES_DIRS = [os.path.join(BASE_DIR, 'mysite/static'), os.path.join(BASE_DIR, 'static/')]

为什么只在调试模式下定义静态文件?您还需要在生产中定义这些

相关问题 更多 >