django-compressor compiles SCSS files in STATIC_ROOT/app instead of app/static

时间:2021-05-03 23:01:40

We're using django-compressor and the django.contrib.staticfiles app and we're having problems while running the django development server and working on our SCSS: the wrong SCSS files gets compiled. The versions that are in STATIC_ROOT/app are getting found rather than the versions in app/static. This makes it so that edits to SCSS in app/static aren't reflected in the compiled CSS.

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

Removing everything in STATIC_ROOT/app fixes the issue, but it causes much confusion if collectstatic is executed for some reason.

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

Is there a way to ensure that the app/static files are compiled rather than any existing STATIC_ROOT/app files?

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

We're using django-compressor 1.4 with django 1.6 and the following settings are used in the django settings file:

我们在django 1.6中使用django-compressor 1.4,并在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')

2 个解决方案

#1


1  

Your sass --scss comand in COMPRESS_PRECOMPILERS does not explicitly state a target directory. Hence the default is used, which seems to be stdin and stdout.

COMPRESS_PRECOMPILERS中的sass --scss命令没有显式声明目标目录。因此使用默认值,似乎是stdin和stdout。

Now, compressor documentation is not so clear what using stdout means; but, from the examples it seems the files will end up in COMPRESS_ROOT (defaults to STATIC_ROOT/CACHE which in your case is root/base/static/CACHE/)

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

What I personally like is to explicitly state the in/out directories (to remain constant in different environments). Here is an example (using a pyScss compiler, but the idea is the same):

我个人喜欢的是明确说明输入/输出目录(在不同环境中保持不变)。这是一个例子(使用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),
)

(sorry if digging up long forgotten issues)

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

#2


0  

Use django-libsass:

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

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

Make sure to configure STATIC_URL and STATIC_ROOT correctly as described in https://docs.djangoproject.com/en/1.8/howto/static-files/.

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

For example:

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 will take care of the rest, appropriately depending on the DEBUG variable.

压缩器将根据DEBUG变量适当地处理其余部分。

#1


1  

Your sass --scss comand in COMPRESS_PRECOMPILERS does not explicitly state a target directory. Hence the default is used, which seems to be stdin and stdout.

COMPRESS_PRECOMPILERS中的sass --scss命令没有显式声明目标目录。因此使用默认值,似乎是stdin和stdout。

Now, compressor documentation is not so clear what using stdout means; but, from the examples it seems the files will end up in COMPRESS_ROOT (defaults to STATIC_ROOT/CACHE which in your case is root/base/static/CACHE/)

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

What I personally like is to explicitly state the in/out directories (to remain constant in different environments). Here is an example (using a pyScss compiler, but the idea is the same):

我个人喜欢的是明确说明输入/输出目录(在不同环境中保持不变)。这是一个例子(使用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),
)

(sorry if digging up long forgotten issues)

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

#2


0  

Use django-libsass:

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

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

Make sure to configure STATIC_URL and STATIC_ROOT correctly as described in https://docs.djangoproject.com/en/1.8/howto/static-files/.

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

For example:

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 will take care of the rest, appropriately depending on the DEBUG variable.

压缩器将根据DEBUG变量适当地处理其余部分。