2014-11-06 38 views
5

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

删除STATIC_ROOT/app中的所有内容解决了该问题,但如果由于某种原因执行collectstatic,则会引起很多混淆。

有没有办法确保应用程序/静态文件被编译而不是现有的STATIC_ROOT /应用程序文件?

我们使用Django的压缩机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') 
+0

你可以分享你的'STATICFILES_DIRS'和'STATIC_ROOT'设置吗? – richardcornish 2014-11-07 15:53:11

+0

刚用'STATICFILES_DIRS'(这只是默认值)和'STATIC_ROOT'编辑。 – mstringer 2014-11-07 20:31:31

+0

究竟是什么'app'?它是一个Django应用程序,一个Django项目或其他东西? 'app/static'在哪里?它被添加到'INSTALLED_APPS'? – richardcornish 2014-11-07 22:03:56

回答

1

sass --scss COMAND在COMPRESS_PRECOMPILERS没有明确的目标目录。因此默认使用,其中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), 
) 

(抱歉,如果挖掘早已被人遗忘的问题)

0

使用django-libsass

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

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

确保正确配置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变量。