我的文件较少,如下所示
@static_url: "/static/"; //don't know how to use static tag or {{STATIC_URL}}
@import url("@{static_url}site_common/bower_components/bootstrap/less/bootstrap.less");
@import url("@{static_url}momsplanner/custom_bootstrap/custom-variables.less");
@import url("@{static_url}momsplanner/custom_bootstrap/custom-other.less");
@import url("@{static_url}site_common/bower_components/bootstrap/less/utilities.less");
它工作正常,但是当我尝试用lessc编译它时,它是可行但非常混乱(我必须首先执行collectstatic,并将STATIC_ROOT作为lessc的include-path选项)
我想在上面较少的文件中使用相对路径比这更容易,还有其他选择吗?
答案 0 :(得分:1)
我建议使用相对的@imports。我编写了一个辅助函数来构造来自配置的django INSTALLED_APPS的包含路径,因为我们正在使用AppDirectoriesFinder
,你可以适应你的手动反编译过程(我们使用django-compressor):
from compressor.filters.base import CompilerFilter
from django.utils.functional import memoize
_static_locations = {}
def _get_static_locations():
from django.conf import settings
"""
Captures all the static dirs (both from filesystem and apps) for build an include path for the LESS compiler.
"""
dirs = ['.']
for dir in settings.STATICFILES_DIRS:
dirs.append(dir)
for app in settings.INSTALLED_APPS:
from django.utils.importlib import import_module
import os.path
mod = import_module(app)
mod_path = os.path.dirname(mod.__file__)
location = os.path.join(mod_path, "static")
if os.path.isdir(location):
dirs.append(location)
return dirs
get_static_locations = memoize(_get_static_locations, _static_locations, 0)
class LessCompilerFilter(CompilerFilter):
def __init__(self, content, command=None, *args, **kwargs):
command = 'lessc --no-color --include-path=%s {infile} {outfile}' % ':'.join(get_static_locations())
super(LessCompilerFilter, self).__init__(content, command, *args, **kwargs)