gpt4 book ai didi

django - 如何使用 jinja2 模板调用 django-pipeline compressor

转载 作者:行者123 更新时间:2023-12-05 07:55:10 24 4
gpt4 key购买 nike

我希望为 Google App 引擎压缩我基于 Jinja2 的 Python 项目中的 JS 和 CSS。我已经安装了 django-pipeline,并将其添加到我的项目路径中。

一些 documentation我不清楚。特别是 Jinja2 的用法。

对于 Django 模板,在 base.html 中 example , 插入:

{% load compressed %}
{% compressed_css 'colors' %}
{% compressed_js 'stats' %}

我假设 Jina2 的等价物是这样的

{{ compressed_css("main") }}
{{ compressed_js("main") }}

但这给出了UndefinedError: 'compressed_css' 未定义

我的问题是如何在 Jinja2 中加载“压缩”模板?这与 Django 的方式不同,我找不到示例。

文档也说

In order to use Django Compressor’s Jinja2 extension we would need to pass compressor.contrib.jinja2ext.CompressorExtension into environment:

我做到了。

import jinja2
from compressor.contrib.jinja2ext import CompressorExtension
env = jinja2.Environment(extensions=[CompressorExtension])

文档也说明了

"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."

我不确定我是否需要在这里做任何事情。

我的 setings.py 包含以下语句:

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'
}
}

最佳答案

将描述在本地环境(不是 GAE)上对我有用的东西。

如果你关注django-pipeline official guide它指出,您应该将 pipeline.templatetags.ext.PipelineExtension 添加到您的环境中,这对我来说意味着将我的 settings.py 更新为:

# myproject/settings.py
TEMPLATES = [
{
...
},
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'environment': 'myproject.jinja2.environment',
'extensions': ['pipeline.templatetags.ext.PipelineExtension']
}
}
]

如何迁移您的模板?对我来说,这非常简单,我只是从模板顶部删除了 {% load pipeline %} 就可以了。

关于django - 如何使用 jinja2 模板调用 django-pipeline compressor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30325130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com