简体   繁体   English

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

[英]Django: ValueError: Missing staticfiles manifest entry for 'jquery.twbsPaginationAlt.min.js'

Before marking as duplicate, note that I've checked all the similar questions that have been asked and it has not fixed my problem.在标记为重复之前,请注意我已经检查了所有被问到的类似问题,但它没有解决我的问题。 I've deployed my Django App on Heroku in in one template where I reference a min.js file, it throws this error in question.我已经在一个引用 min.js 文件的模板中的 Heroku 上部署了我的 Django 应用程序,它抛出了这个有问题的错误。

My basic directory structure is this:我的基本目录结构是这样的:

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

settings.py:设置.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)

So originally if STATICFILES_DIRS had any value, I would get an error of FileNotFoundError: [Errno 2] No such file or directory: '/tmp/build_6f3eb879/mysite/static' when running collectstatic on heroku. However, if it is not set, than the fonts I have in my dev environment do not load in properly (I also have fonts inside the base level static folder).所以最初如果 STATICFILES_DIRS 有任何值,我会得到一个错误FileNotFoundError: [Errno 2] No such file or directory: '/tmp/build_6f3eb879/mysite/static' when running collectstatic on heroku. 但是,如果它没有设置,比我在我的开发环境中的 fonts 没有正确加载(我在基本级别 static 文件夹中也有 fonts)。

This is how I reference the js file in my problem template, it works fine on dev:这就是我在问题模板中引用 js 文件的方式,它在 dev 上运行良好:

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

For convenience, here is what django-heroku does in terms of staticfiles:为方便起见,以下是 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'

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

EDIT: In order to fix this I had to stop using django heroku, change my STATICFILES_STORAGE variable to whitenoise.storage.CompressedStaticFilesStorage, and reorganize where I kept some of my static files by checking the logs to see what path the GET requests were going to.编辑:为了解决这个问题,我不得不停止使用 django heroku,将我的 STATICFILES_STORAGE 变量更改为 whitenoise.storage.CompressedStaticFilesStorage,并通过检查日志以查看 GET 请求的路径来重新组织我保存一些 static 文件的位置. It seems whitenoise is pretty imperfect based off the other stackoverflow posts I've seen, it consistently misses 'static/' files in the root directory.根据我见过的其他 stackoverflow 帖子,whitenoise 似乎非常不完美,它始终会丢失根目录中的“static/”文件。 Additionally since Django relies a lot on the 'static' keyword in templates, and this is not available in.css files, the only way to resolve getting static files into a css file (for fonts for example), is trial and error of testing different paths.此外,由于 Django 很大程度上依赖模板中的“static”关键字,而这在 .css 文件中不可用,解决将 static 文件转换为 css 文件(例如 fonts)的唯一方法是反复试验不同的路径。

This line looks very odd to me and may be the source of the problem:这条线对我来说看起来很奇怪,可能是问题的根源:

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

Why are you only defining your STATICFILES_DIRS in debug mode?为什么只在调试模式下定义 STATICFILES_DIRS? You'll need those defined in production too.您也需要在生产中定义的那些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM