I have the following model:
class Messages(db.Model):
name: db.StringProperty()
...
which uses the id field provided by the datastore of Google App Engine as a unique key, and it can be obtained by:
message = Message()
...
message.put()
...
id = message.key().id()
Now I would like to list the messages using the Django template system:
messages = db.Messages.all()
self.response.out.write(template.render('page.html', 'messages': messages))
And in the ‘page.html’:
{% for message in messages %}
{{message.name}}
{{message.id}}
....
{% endfor %}
However, the ‘message.id’ is always empty in the above loop. How to I pass the entity id’s to the template system such that I may use them in the web page? Thanks.
The template code is no different in that you still need to access the
idinstance-method of the key.Try instead:
The syntax, if you use Jinja2 templates (instead of Django templates), would be: