Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在GAE 1.6.0中添加自定义Jinja2过滤器_Python_Google App Engine_Jinja2 - Fatal编程技术网

Python 在GAE 1.6.0中添加自定义Jinja2过滤器

Python 在GAE 1.6.0中添加自定义Jinja2过滤器,python,google-app-engine,jinja2,Python,Google App Engine,Jinja2,我想添加过滤器来格式化我的时间,最好是像django的timesince这样的过滤器,它可以自动输出i18n所选语言的语言,但首先要快速解决我想格式化我的日期。最重要的是: 但是,将此代码添加到我的文件不会使筛选器在模板中可用: {{ ad.modified|datetimeformat }} TemplateAssertionError: no filter named 'datetimeformat' 如果我将代码添加到Jinja2库的filters.py,那么它就可以工作了。但是我不需要

我想添加过滤器来格式化我的时间,最好是像django的
timesince
这样的过滤器,它可以自动输出i18n所选语言的语言,但首先要快速解决我想格式化我的日期。最重要的是:

但是,将此代码添加到我的文件不会使筛选器在模板中可用:

{{ 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

按照您的示例,我添加了自定义过滤器,它可以正常工作。 确保使用适当的
jinja2.Environment
实例获取模板和渲染:

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)

因为我使用的是推荐的缓存jinja2环境

Kee的回答对我不起作用,但确实起了作用

具体来说,在调用
webapp2.WSGIApplication

myconfig = {}
myconfig['webapp2_extras.jinja2'] =  {'template_path': ['templates','blog_posts'],
                                      'filters': {'blog_filter': blog_filter}}

app = webapp2.WSGIApplication(_routes,
    debug=True,
    config = myconfig)

在这种情况下,jinja_环境是什么?你确定这就是你用来渲染的那个吗?你能链接到建议这样做的文档吗?
myconfig = {}
myconfig['webapp2_extras.jinja2'] =  {'template_path': ['templates','blog_posts'],
                                      'filters': {'blog_filter': blog_filter}}

app = webapp2.WSGIApplication(_routes,
    debug=True,
    config = myconfig)