如何在 Google App Engine 模板系统中注册自定义过滤器? [英] How do I register custom filter in Google App Engine template system?

查看:23
本文介绍了如何在 Google App Engine 模板系统中注册自定义过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Django 文档,我已经注册了我的过滤器:

According to Django documentation I've registered my filter:

from google.appengine.ext.webapp import template
# ...
register = template.create_template_register()
@register.filter(name='wld')
def wld(result):
    if result == 1 : return "win"
    if result == 0 : return "loss"
    if result == 0.5 : return "draw"
    return "unknown"
self.response.out.write(template.render("player.html", template_values))

模板中的某处我有代码:
{{result|wld}}

somewhere in the template I have code:
{{result|wld}}

当我尝试渲染模板时,出现错误:TemplateSyntaxError: Invalid filter: 'wld'

and when I try to render my template, I get the error: TemplateSyntaxError: Invalid filter: 'wld'

我做错了什么?

推荐答案

创建自定义标记库后,需要将其注册到 Django 模板引擎:

Once you have created your custom tag library, you need to register it with the Django template engine:

from google.appengine.ext.webapp import template
template.register_template_library('path.to.lib')

请注意,调用 template.register_template_library 是作为 AppEngine SDK 的一部分提供的包装器.一旦你把它放在你的 main.py 中,新的标签或过滤器应该可以在你的所有模板中使用,无需任何进一步的工作.无需使用 {% load %} 标签.

Note that the call template.register_template_library is a wrapper that is provided as part of the AppEngine SDK. Once you have put this in your main.py, the new tags or filters should be available in all of your templates without any further work. No need to use the {% load %} tag.

重要说明:register_template_library 的功能会因您在 AppEngine 应用程序中使用的 Django 版本而异.如果您使用的是 0.96,则该参数将是自定义标签库文件的路径.如果您使用的是 Django 1.2,它将通过一个 python 模块路径指向自定义标记库.我在我博客上的一篇文章中发布了如何完成这项工作的说明 .

An important note: the functioning of register_template_library will vary depending on which version of Django you are using in your AppEngine application. If you are using 0.96, the parameter will be the path to the custom tags library file. If you are using Django 1.2, it will by a python module path to the custom tag library. I posted instructions for making this work in a post on my blog.

这篇关于如何在 Google App Engine 模板系统中注册自定义过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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