问题描述
方法是:
def printa(x):
return x
响应为:
self.response.out.write(template.render(path, {'printa':printa}))
html是:
{{ printa 'sss'}}
我想在页面中显示"sss",
那怎么做,
已更新
我创建了一个templatetags文件夹和2个py文件:
templatetags
|--------__init__.py
|--------tags.py
tags.py中的是:
#from django import template
from google.appengine.ext.webapp import template
register = template.Library()
def printa():
return 'ssss'
register.simple_tag(printa)
html是:
{% load tags%}
{% printa %}
但是显示错误,错误是:
TemplateSyntaxError: 'tags' is not a valid tag library: Could not load template library from django.templatetags.tags, No module named tags
为什么?
怎么了?
答案是:
tags.py:
from google.appengine.ext import webapp
register = webapp.template.create_template_register()
@register.filter
def printa(value,y):
return 'url:%s' % y
@register.tag
def printb(x,y):
return str(x.__dict__) +'dddddddddddddddddddddddddddddddd'+ str(y.__dict__)
#return x
#register.tag('printb',do_pagednav)
,然后在html中(a是我发送到模板的变量):
{{a|printa:"dwqdq"}}
{% printb %}
担心:
请勿使用负载:
{% load sometags %}
使用默认的webapp模板系统(实际上是Django 0.96),您无法执行此操作.您应该将程序逻辑放在程序文件中,而不是模板中,这样就不能将参数传递给变量.
但是,您没有说出您实际上在试图做什么.我假设您实际上并不希望打印某些内容,因为您可以将这些内容放入不带函数的模板中并打印出来.无论您实际上要做什么,都可以注册自定义过滤器可能就是您要寻找的. p>
the method is:
def printa(x):
return x
the response is:
self.response.out.write(template.render(path, {'printa':printa}))
the html is:
{{ printa 'sss'}}
I want to show 'sss' in my page ,
so how to do this ,
updated
I create a templatetags folder, and 2 py file:
templatetags
|--------__init__.py
|--------tags.py
in tags.py is:
#from django import template
from google.appengine.ext.webapp import template
register = template.Library()
def printa():
return 'ssss'
register.simple_tag(printa)
the html is:
{% load tags%}
{% printa %}
but it show error, and the error is:
TemplateSyntaxError: 'tags' is not a valid tag library: Could not load template library from django.templatetags.tags, No module named tags
why?
what's wrong?
answer is:
tags.py:
from google.appengine.ext import webapp
register = webapp.template.create_template_register()
@register.filter
def printa(value,y):
return 'url:%s' % y
@register.tag
def printb(x,y):
return str(x.__dict__) +'dddddddddddddddddddddddddddddddd'+ str(y.__dict__)
#return x
#register.tag('printb',do_pagednav)
and then in html (a is a variable i send to the template):
{{a|printa:"dwqdq"}}
{% printb %}
woring:
don't use load:
{% load sometags %}
Using the default webapp template system (which is actually Django 0.96), you can't do this. You're expected to put the program logic in the program files, not in your templates, so you can't pass arguments to your variables.
You don't say what you're actually trying to do, though; I assume you don't literally want to print something, since you can just put that something in the template without a function and it prints. For whatever you're actually trying to do, registering a custom filter might be what you're looking for.
这篇关于如何在google-app-engine模板中启用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!