Life is Simple

  • Random
  • Archive

Acoustic theme by Rolando Murillo, using the iPhone toolbar icons.

  • Permalink

    Assuming I have the following:

    class Person(db.Model):
      name
    = db.StringProperty()

    I would like to print all the names in an html file using a template.

    template_values = {'list': Person.all()}

    And the template will look like this:

    {% for person in list %}
    <form>
     
    <p>{{ person.name}} </p>
      <button type="button" name="**{{ person.id }}**">Delete!</
    button>
    </form>
    {% endfor %}

    Ideally I would like to use person.key or person.id to then be able to delete the record using the key but that doesn’t seem to work. Any Ideas how can I accomplish this?

    python google-app-engine
    link|edit|flag
    edited Oct 31 ‘10 at 0:39

    asked Oct 30 ‘10 at 17:58
    Luis Benavides
    113

    2 Answers

    active oldest votes
    up vote 2 down vote

    Use {{person.key.id}}, not just {{id}}. This will call each object’s .key().id() method(s).

    However, you should also be aware that passing Person.all() as a template value isn’t necessarily a great idea; .all() returns a db.Query object, which can be treated as an iterable like you’re doing but which will do multiple RPCs as you iterate through the query; instead you should use something like Person.all().fetch(SOME_NUMBER), where SOME_NUMBER is a reasonable amount to display to the user (or an arbitrarily large number if you insist on trying to display everything in one view.)

    — python - Google App Engine: Get entity key to use in a template - Stack Overflow