在 GAE 1.6.0 中添加自定义 Jinja2 过滤器
我想添加过滤器来格式化我的时间,最好的过滤器是像 django 的 timesince
这样的过滤器,它会自动输出 i18n 所选语言的语言,但首先要做一个快速的解决方案,我想格式化我的日期。 手册中建议的解决方案是:
def datetimeformat(value, format='%H:%M / %d-%m-%Y'):
return value.strftime(format)
jinja_environment.filters['datetimeformat'] = datetimeformat
但是将此代码添加到我的文件中并不不使过滤器在模板中可用:
{{ ad.modified|datetimeformat }}
TemplateAssertionError: no filter named 'datetimeformat'
如果我将代码添加到 Jinja2 库的 filters.py
中,那么它就可以工作。但我不需要手动添加到 Jinja2 文件,只需将 Jinja2 添加到我的 app.yaml
并将过滤器放入我的代码中而不是 Jinja2 代码中即可。过滤器代码应该放在哪里?
谢谢
更新
我的代码如下所示,似乎没有选择过滤器:
from django.utils import translation
from django.utils.translation import gettext, ngettext, ugettext, ungettext, get_language, activate
from jinja2 import Environment, FileSystemLoader
class DjangoTranslator(object):
def __init__(self):
self.gettext = gettext
self.ngettext = ngettext
self.ugettext = ugettext
self.ungettext = ungettext
class DjangoEnvironment(jinja2.Environment):
def get_translator(self, context):
return DjangoTranslator()
jinja_environment = DjangoEnvironment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), extensions=['jinja2.ext.i18n'])
jinja_environment.install_gettext_translations(translation)
def datetimeformat(value, format='%H:%M / %d-%m-%Y'):
return value.strftime(format)
jinja_environment.filters['datetimeformat'] = datetimeformat
I'd like to add filter to format my time and the best would be filters like django's timesince
that automatically outputs the language of the i18n selected language, but first to make a quick solution I'd like to format my date. The suggested solution from the manual is:
def datetimeformat(value, format='%H:%M / %d-%m-%Y'):
return value.strftime(format)
jinja_environment.filters['datetimeformat'] = datetimeformat
But adding this code to my file doesn't make the filter available in the template:
{{ ad.modified|datetimeformat }}
TemplateAssertionError: no filter named 'datetimeformat'
If I add the code to the Jinja2 library's filters.py
then it works. But I shouldn't need to add to Jinja2 files manually, it should work just adding the Jinja2 to my app.yaml
and put my filter in my code instead of in the Jinja2 code. Where should I put the filter code?
Thank you
Update
My code looks like this and it seems the filter is not picked up:
from django.utils import translation
from django.utils.translation import gettext, ngettext, ugettext, ungettext, get_language, activate
from jinja2 import Environment, FileSystemLoader
class DjangoTranslator(object):
def __init__(self):
self.gettext = gettext
self.ngettext = ngettext
self.ugettext = ugettext
self.ungettext = ungettext
class DjangoEnvironment(jinja2.Environment):
def get_translator(self, context):
return DjangoTranslator()
jinja_environment = DjangoEnvironment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), extensions=['jinja2.ext.i18n'])
jinja_environment.install_gettext_translations(translation)
def datetimeformat(value, format='%H:%M / %d-%m-%Y'):
return value.strftime(format)
jinja_environment.filters['datetimeformat'] = datetimeformat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按照您的示例和 Jinja2 文档 我添加了自定义过滤器并且它有效。
确保使用正确的 jinja2.Environment 实例来获取模板和渲染:
Following your example and Jinja2 docs I've added custom filter and it works.
Make sure that you use proper
jinja2.Environment
instance for getting template and rendering:因为我按照此处的建议使用缓存的jinja2环境,
Kee的答案不适合我,但这个做到了。
具体来说,在调用
webapp2.WSGIApplication
时添加过滤器Because I was using a cached jinja2 environment as recommended here,
Kee's answer didn't work for me, but this one did.
Specifically, adding the filter when calling
webapp2.WSGIApplication