【问题标题】:django-compressor compiles SCSS files in STATIC_ROOT/app instead of app/staticdjango-compressor 在 STATIC_ROOT/app 而不是 app/static 中编译 SCSS 文件
【发布时间】:2016-05-12 20:39:46
【问题描述】:

我们正在使用 django-compressordjango.contrib.staticfiles 应用程序,在运行 django 开发服务器和处理 SCSS 时遇到问题:编译了错误的 SCSS 文件。正在找到 STATIC_ROOT/app 中的版本,而不是 app/static 中的版本。这使得在app/static 中对 SCSS 的编辑不会反映在编译后的 CSS 中。

删除STATIC_ROOT/app 中的所有内容可以解决此问题,但如果出于某种原因执行collectstatic,则会引起很多混乱。

有没有办法确保编译 app/static 文件而不是任何现有的 STATIC_ROOT/app 文件?

我们将 django-compressor 1.4 与 django 1.6 一起使用,并且在 django 设置文件中使用了以下设置:

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    'compressor.finders.CompressorFinder',
)
COMPRESS_PRECOMPILERS = (
    ("text/x-scss", 'sass --scss'),
)
STATICFILES_DIRS = [] #default
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

【问题讨论】:

  • 您能分享一下您的STATICFILES_DIRSSTATIC_ROOT 设置吗?
  • 刚刚使用STATICFILES_DIRS(这只是默认设置)和STATIC_ROOT编辑。
  • app 到底是什么?它是一个 Django 应用程序、一个 Django 项目还是其他什么? app/static 住在哪里?并添加到INSTALLED_APPS
  • 感谢 cmets! app 是我们在项目中使用的任何 Django 应用程序(它们在 INSTALLED_APPS 中)。例如,我们在PROJECT_ROOT/base 中有一个名为base 的应用,在PROJECT_ROOT/base/static 中有静态文件(我们的项目布局类似于this one

标签: python django django-compressor


【解决方案1】:

COMPRESS_PRECOMPILERS 中的 sass --scss 命令没有明确声明目标目录。因此使用默认值,seems to be stdin and stdout

现在,压缩器文档并不清楚使用 stdout 的含义;但是,从示例看来,文件最终将以COMPRESS_ROOT 结尾(默认为STATIC_ROOT/CACHE,在您的情况下为root/base/static/CACHE/

我个人喜欢的是明确说明输入/输出目录(在不同环境中保持不变)。这是一个示例(使用pyScss 编译器,但思路相同):

scss_cmd = '{python} -mscss -A "{image_output_path}" -a "{static_url}" ' \
    '-S "{static_root}" -o "{{outfile}}" "{{infile}}"'.format(
        python=sys.executable,
        image_output_path=COMPRESS_ROOT,
        static_url=STATIC_URL,
        static_root=os.path.join(PROJECT_ROOT),
    )

COMPRESS_PRECOMPILERS = (
    ('text/x-scss', scss_cmd),
)

(对不起,如果挖掘长期被遗忘的问题)

【讨论】:

    【解决方案2】:

    使用django-libsass:

    COMPRESS_PRECOMPILERS = (
        ('text/x-sass', 'django_libsass.SassCompiler'),
        ('text/x-scss', 'django_libsass.SassCompiler'),
    )
    

    https://github.com/torchbox/django-libsass

    确保按照https://docs.djangoproject.com/en/1.8/howto/static-files/ 中的说明正确配置STATIC_URLSTATIC_ROOT

    例如:

    STATIC_URL = '/static/'
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, "static"),
    )
    STATIC_ROOT = os.path.join(BASE_DIR, 'static_collected')
    
    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
        'compressor.finders.CompressorFinder',
    )
    

    Compressor 将负责其余的工作,具体取决于 DEBUG 变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 2018-07-08
      • 2013-10-21
      • 2017-05-18
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      相关资源
      最近更新 更多