在GAE 1.6.0中添加一个自定义的Jinja2过滤器 [英] Adding a custom Jinja2 filter in GAE 1.6.0

查看:118
本文介绍了在GAE 1.6.0中添加一个自定义的Jinja2过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加过滤器来设置我的时间格式,最好的过滤器是像django的 timesince 这样的过滤器,可以自动输出i18n所选语言的语言,快速解决方案我想格式化我的日期。 手册中的建议解决方案为:

  def datetimeformat(value,format ='%H:%M /%d-%m-%Y'):
return value.strftime(格式)

jinja_environment.filters ['datetimeformat'] = datetimeformat

但将此代码添加到我的文件中并不会使模板中的过滤器可用:

  {{ad.modified | datetimeformat}} 
TemplateAssertionError:没有过滤器名为'datetimeformat'

如果我将代码添加到Jinja2库 filters.py 然后它可以工作。但我不需要手动添加Jinja2文件,只需将Jinja2添加到我的 app.yaml 中,然后将我的过滤器放入我的代码而不是Jinja2中码。

谢谢

更新

p>

我的代码看起来像这样,它似乎没有拿起过滤器:

 从django.utils导入翻译
from django.utils.translation import gettext,ngettext,ugettext,ungettext,get_language,激活jinja2中的
导入Environment,FileSystemLoader
$ b $ class DjangoTranslator (object):
def __init __(self):
self.gettext = gettext
self.ngettext = ngettext
self.ugettext = ugettext
self.ungettext = ungettext
$ b $ class DjangoEnvironment(jinja2.Environment):
def get_translator(self,context):
返回DjangoTranslator()

jinja_environment = DjangoEnvironment(
loader = jinja2.FileSystemLoader(os.path.dirname(__ file__)),extensions = ['jinja2.ext.i18n'])
jinja_environment.install_gettext_translations(translation)

def datetimetimet(value,format ='%H:%M /%d-%m-%Y'):
返回value.strftime(格式)

jinja_environment。 filters ['datetimeformat'] = datetimeformat


解决方案

Jinja2文档我已经添加了自定义过滤器并且它可以工作。
请确保您使用正确的 jinja2.Environment 实例获取模板和渲染:

  env = jinja2.Environment(
loader = jinja2.FileSystemLoader(template_path))
env.filters ['default_if_none'] = default_if_none#a函数
tmpl = env .get_template(filename)
tmpl.render(** context)


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

解决方案

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:

env = jinja2.Environment(
    loader=jinja2.FileSystemLoader(template_path))
env.filters['default_if_none'] = default_if_none  # a function
tmpl = env.get_template(filename)
tmpl.render(**context)

这篇关于在GAE 1.6.0中添加一个自定义的Jinja2过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆