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

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

问题描述

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

  from google.appengine.ext.webapp import template 
$ ...
register = template.create_template_register()
@ register.filter(name ='wld')
def wld(result):
if result == 1:returnwin
if result == 0:returnloss
if result == 0.5:returndraw
returnunknown
self.response。 out.write(template.render(player.html,template_values))

模板中的某处我有代码:

{{result | wld}}



,当我尝试要渲染我的模板,我得到错误: TemplateSyntaxError:无效过滤器:'wld'



我在做什么

解决方案

创建自定义标签库后,需要使用Django模板引擎注册:

 来自google.appengine.ext.webapp import t emplate 
template.register_template_library('path.to.lib')

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



一个重要的说明: register_template_library 将根据您在AppEngine应用程序中使用的Django版本而有所不同。如果使用0.96,参数将是自定义标签库文件的路径。如果你使用Django 1.2,它将通过python模块路径到自定义标签库。我发布了进行此工作的说明在我的博客上的一篇文章


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

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

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

What am I doing wrong?

解决方案

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')

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.

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天全站免登陆