- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望为 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/
这是我的代码: {% if {{post.author.profile.image.url}} is None %} 当我运行这段代码时出现错误 Could not parse the remaind
我想将 jinja 循环内的单个 jinja 变量作为参数传递给 python 函数。在这种情况下,我需要将 jinja 循环内的 {{ book.isbn }} 传递给 python 函数才能使用该
问题几乎在标题中。 IntelliJ 是否具有针对 .jinja 的 native 语法高亮显示?和 .j2文件? 如果没有,是否有插件? 最佳答案 这个插件:https://plugins.jetb
我只想循环现有列表并从中创建一个逗号分隔的字符串。 像这样的东西:my_string = 'stuff,stuff,stuff,stuff' 我已经了解loop.last,我只需要知道如何使下面代码中
我在 HTML 表格中添加了复选框,我想知道是否选中了任何复选框。我有一个在 Jquery 中运行的脚本,它告诉我变量中标记的复选框的数量,但我还没有找到如何在 Jinja if 语句条件中使用 Jq
嗨,我觉得我需要为我现在挣扎了几个小时的事情提供答案。我希望有一天这对某人有所帮助。 我一直在尝试自动化 Ansible 部署的模板: 库存内容: [splunk_license] 10.10.113
我想记录我的 Jinja 宏是如何工作的。在 Jinja 中是否有用于记录宏的官方格式,就像有多种格式化 Python 文档字符串的方法一样? 也许是这样的: {% macro my_macro(ar
有没有办法加载自定义标签集 - 甚至是单个自定义标签? 在 django 模板中,我可以这样做: {% load custom_tag_set %} 有没有办法在神社中做到这一点? 最佳答案 存在相同
我正在创建一种表单,用户可以在其中添加一个或两个额外的字段...我正在使用jQuery进行交互,并使用Flask进行模板化,它看起来像这样: Add College or University
所以我试图在 url_for 中嵌套 2 个变量来提取特定人物的照片。这是我的代码 我的图像采用“用户名.png”格式,因此我在开头和结尾添加“_”。 这是它的Python部分: @app.rout
我想做的基础是使用“随机”过滤器从我的列表中选择一个随机项目,但随后我想在多个位置使用该随机选择的项目。 如何将过滤器的结果设置为可以在多个位置使用的变量。 如果我多次调用“随机”过滤器,它们几乎不可
我想知道在文件中添加包含或宏时是否有任何方法可以保持 jinja 的缩进。我想使用 jinja 生成代码文件。一个例子是 文件:class.html class MyClass: def s
我已经能够使用 BaseOperator 中的函数 render_template 成功渲染 Jinja 模板。 我的问题是,有谁知道在Rendered或Rendered Template选项卡下将渲
我从以下 ninja2 模板开始,但如果第二列的值 > 指定的宽度,它只会打印整个字符串,其余列右移。 {{ "%-7s"|format(tbl[3]) }} {{ "%-12s"|form
我一直在尝试使用 Flask-Cache 的 memoize 功能来仅返回 statusTS() 的缓存结果,除非在另一个请求中满足特定条件,然后删除缓存。 但它并没有被删除,并且 Jinja 模板仍
在 jinja 文件中找到其中一些: {%- else -%} 还有 {% if freeswitch_dispatcher -%} 看到破折号了吗?知道它的用途吗? 最佳答案 结果是 + 和
我从以下 ninja2 模板开始,但如果第二列的值 > 指定的宽度,它只会打印整个字符串,其余列右移。 {{ "%-7s"|format(tbl[3]) }} {{ "%-12s"|form
好的,所以我有了第一个 HTML 文件(header.html): M4A {% load staticfiles %} {% block content %}
我有一个看起来像这样的Python字典 [{"hat": 91, "d1b": 2, "d1c": 1, "d1d": 5, "d1e": 7, "d1f": 77, "d1e": 999} {"ha
我正在使用 SaltStack 来管理 BIND9 区域文件。以前我使用过这样的支柱数据: zones: example.com: a: www1: 1.2.3.4
我是一名优秀的程序员,十分优秀!