Is it possible to access the key_name of an object from inside a jinja2 template?

ε祈祈猫儿з 提交于 2019-12-23 07:47:55

问题


I'm using python google app engine with the webapp framework, substituting jinja2 templates for django templates.

I know you can get the key_name of an object from inside a handler method by calling it like so:

goody = object.key().name()

But is it possible to access the key_name from within the template logic? Something like this?

{% for object in objectList %}
{{object|key_name}}
{% endfor %}

Obviously that doesn't work, but is there some filter or hidden attribute I can use to get at the app engine key_name an object is stored with from inside the template without extra fiddling inside the handler method?


回答1:


Jinja2 allows you to execute callables, just by using () according to the documentation here: http://jinja.pocoo.org/docs/templates/#other-operators

So, this should work:

{% for instance in instance_list %}
   {{ instance.key().name() }}
{% endfor %}



回答2:


You can do object.key.name inside django template to access appengine datastore object's key name

Normally, you access key name in python code through object.key().name()

Django template automatically tries a few possible ways to interpret object.key.name to give you the value.



来源:https://stackoverflow.com/questions/5190204/is-it-possible-to-access-the-key-name-of-an-object-from-inside-a-jinja2-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!