nhu*_*uon 10 python django google-app-engine
我正在尝试使用Google App Engine创建Django自定义标签,但由于某种原因它不能一直运行.我相信我的标签已正确注册,因为Django正在解析它们,但从不调用render方法.最奇怪的是,我的标签在放置在for循环中时起作用{%for ...%}但从不在外面.
这是代码:
在django/mytags.py中
from django import template
from google.appengine.ext import webapp
register = webapp.template.create_template_register()
# This works all the time
@register.simple_tag
def hello_world():
return u'Hello world'
@register.tag('foo')
def foo(parser, token):
return FooNode()
class FooNode(template.Node):
def __init__(self):
self.foo = 'foo'
def render(self, context):
return self.foo
Run Code Online (Sandbox Code Playgroud)
在main.py中
from google.appengine.ext.webapp import template
template.register_template_library('django.mytags')
...
self.response.out.write(template.render('main.html', template_values))
Run Code Online (Sandbox Code Playgroud)
在main.html中
{% foo %}
{% for item in items %}
{% foo %}
Run Code Online (Sandbox Code Playgroud)
结果:
<django.mytags.FooNode object at 0x000000001794BAC8>
foo
foo
foo
...
Run Code Online (Sandbox Code Playgroud)
这让我疯了.我怀疑将我的标签放在for循环中强制要渲染节点(它应该已经完成了).
您需要为您的类添加字符串表示形式
class FooNode(template.Node):
def __init__(self):
self.foo = 'foo'
def render(self, context):
return self.foo
def __unicode__(self):
return 'string to put in template'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
439 次 |
| 最近记录: |