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 模板文件中的Google appengine字符串替换_Python_Google App Engine - Fatal编程技术网

Python 模板文件中的Google appengine字符串替换

Python 模板文件中的Google appengine字符串替换,python,google-app-engine,Python,Google App Engine,当然,我使用的是google appengine python:我想对模板文件中的字符串执行string.replace 但这是行不通的。因此,除了在应用程序引擎模板中执行基本检查外,我们无法执行任何其他操作。对吗 另一个相关的问题是,我试图缩短字符串并使其可用于模板 每个家具对象都有一个名称和一个较长的描述字段。在我正在渲染的这个视图中,我只需要描述字段的前50个字符 所以我尝试了类似的方法 items = db.GqlQuery( 'select * from furniture' ) #

当然,我使用的是google appengine python:我想对模板文件中的字符串执行string.replace

但这是行不通的。因此,除了在应用程序引擎模板中执行基本检查外,我们无法执行任何其他操作。对吗

另一个相关的问题是,我试图缩短字符串并使其可用于模板

每个家具对象都有一个名称和一个较长的描述字段。在我正在渲染的这个视图中,我只需要描述字段的前50个字符

所以我尝试了类似的方法

items = db.GqlQuery( 'select * from furniture' ) # edit: if you change the above line to # items = db.GqlQuery( 'select * from furniture' ).fetch( 1000 ) # the .fetch() command makes the addition of dynamic properties work! for item in items : item.shortdescr = item.description[ 0:50 ] # pass data off to template for rendering self.response.out.write( template.render( 'furnitureAll.html', { 'items' : items } ) ) 模板去

{% for item in items %} <p>{{ item.name }}</p> <p>{{ item.shortdescr }}</p> <!-- items.shortdescr does not exist here, probably because I did not .put() it previously. --> {% endfor %} 由于这不起作用,我尝试更改Gql查询以缩短字符串。但我很快意识到Gql不像SQL。我试着写这样的查询

select name,LEFT( description, 50 ) from furniture
除了参数less.fetch调用之外,几乎没有什么成功。在代码中调用fetch调用,我认为这不可能工作。您必须始终通过fetch参数-您愿意获取的最大实体数!,我无法重现您的问题-在我的测试中,将一个新属性(包括通过处理现有属性而获得的属性)分配给每个项目就可以了

你能不能把你观察到的问题复制到尽可能小的罗盘上,并编辑你的问题以包括所有相关文件?看来这是我们唯一能帮你解决你那奇怪的虫子的方法了


顺便说一句,选择名称,左描述,50或其他当然在GQL中不起作用-GQL,非常明确,只支持选择*来获取整个实体,或者选择_u键_;来获取实体的键-仅此而已;select中没有列的选择性,更不用说对它们进行任何操作了-

我对谷歌AppEngine没有什么经验,但我的理解是它与Django有着非常密切的关系。您的模板实际上并不包含Python代码,即使您在模板中使用的某些结构与之类似

这两个问题都应该使用模板过滤器解决。如果是Django,我会用这样的方式回答你的第二个问题:

{{ item.description|truncatewords:10 }}
对于您的第一个问题字符串替换,可能没有可用于该问题的内置筛选器。你需要自己写。像这样的东西

from google.appengine.ext.webapp.template import create_template_register

register = create_template_register()

@register.filter
def replace_underscores(strng):
    return strng.replace('_', ' ')
然后,在模板中,可以执行以下操作:

{{ item.code|replace_underscores }}

内疚的我加了一句。在没有测试b/c的情况下取b/c,这是以前没有的-没有它也可以用,但我想我应该在问题的官方版本中加上它。哦,哇!添加.fetch 100使动态字段的添加生效!如果没有fetch,您还没有列表,那么您就有了一个查询iterable,但没有列表。但是fetch需要一个参数max-numbers-of-items-fetch来返回一个列表。谢谢,伙计。ftr在truncatewords:arg之间似乎应该有一个冒号,比如truncatewords:10。
{{ item.code|replace_underscores }}