我认为这补充了@Yuji 'Tomita' Tomita 的评论,作为一个完整的设置。
这对我有用。
这就是我在 django js 文件中使用 STATIC_URL 完成 django 压缩器的完整设置。
https://github.com/jezdez/django_compressor
pip install django_compressor
将“压缩器”添加到您的 INSTALLED_APPS 设置中:
INSTALLED_APPS = (
# other apps
"compressor",
)
在 Debug 模式下测试压缩,在 settings.py 中:
COMPRESS_ENABLED = True
如果您使用 Django 的 staticfiles contrib 应用程序(或其独立对应的 django-staticfiles),您必须将 Django Compressor 的文件查找器添加到 STATICFILES_FINDERS 设置中,例如使用 django.contrib.staticfiles:
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# other finders..
'compressor.finders.CompressorFinder',
)
安装后在settings.py中启用TemplateFilter,通过模板引擎解析你的js或css文件。
COMPRESS_JS_FILTERS = [
'compressor.filters.template.TemplateFilter',
]
现在 {% compress js %} 块中的任何 JS 都将被 django 模板语言解析...
{% load compress %}
{% compress js %}
<script src="/my_script.js" type="text/javascript"></script>
{% endcompress %}
// my_script.js
alert('{{ STATIC_URL|escapejs }}');