Reputation: 5684
I wish to compress the JS and CSS in my Jinja2 based Python project for Google App engine. I have installed django-pipeline, and added it to my project path.
Some of the documentation is not clear to me. Particularly Jinja2 usage.
For Django Templates, in base.html example, insert:
{% load compressed %}
{% compressed_css 'colors' %}
{% compressed_js 'stats' %}
I assume the equivalent for Jina2 would be something like
{{ compressed_css("main") }}
{{ compressed_js("main") }}
But this gives UndefinedError: 'compressed_css' is undefined
My question is how to load the 'compressed' template in Jinja2? This is not done in the same way as Django and I can't find an example.
The docs also say
In order to use Django Compressor’s Jinja2 extension we would need to pass compressor.contrib.jinja2ext.CompressorExtension into environment:
I have done that.
import jinja2
from compressor.contrib.jinja2ext import CompressorExtension
env = jinja2.Environment(extensions=[CompressorExtension])
The docs also state
"Unlike the Django template tag implementation the Jinja2 implementation uses different templates, so if you wish to override them please override pipeline/css.jinja and pipeline/js.jinja."
I am not sure if I need to do anything here.
My setings.py includes the following statements:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = '{{ project_name }}.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = '{{ project_name }}.wsgi.application'
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, "templates"),
os.path.join(PROJECT_PATH, "templates/includes")
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework_swagger',
'django_jinja.contrib._pipeline',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'pipeline.finders.PipelineFinder'
)
PIPELINE_ENABLE = True
PIPELINE_JS_COMPRESSOR = 'pipeline.compressors.closure.ClosureCompressor'
PIPELINE_CLOSURE_BINARY = 'C:\bunjilsrc\tools\minify\minify.bat'
PIPELINE_DISABLE_WRAPPER = True
PIPELINE_ENABLE_GAE_SUPPORT = True
STATIC_ROOT = 'static/'
STATIC_URL = '/static/'
MEDIA_ROOT = 'uploads/'
MEDIA_URL = "/media/"
PIPELINE_JS = {
'main': {
'source_filenames': (
'js/site.js '
),
'output_filename': 'js/main.js'
},
'vendor': {
'source_filenames': (
'js/vendor/jquery.js',
),
'output_filename': 'js/vendor.js'
}
}
PIPELINE_CSS = {
'main': {
'source_filenames': (
'bootstrap.css',
'site.css'
),
'output_filename': 'css/main.css'
}
}
Upvotes: 3
Views: 1242
Reputation: 878
Going to describe things which worked for me on local env (not GAE).
If you follow django-pipeline official guide it states, that you should add pipeline.templatetags.ext.PipelineExtension
to your environment, which for me meant updating my settings.py
to this:
# myproject/settings.py
TEMPLATES = [
{
...
},
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'environment': 'myproject.jinja2.environment',
'extensions': ['pipeline.templatetags.ext.PipelineExtension']
}
}
]
How to migrate your template? For me it was dead easy, I just removed {% load pipeline %}
from top of the template and it worked.
Upvotes: 0