Question

I am using App Engine to generate a HTML table corresponding to my GQL table. Each HTML row will contain several data and the GQL row ID. This ID in the HTML allows me to delete the item later.

First I give the array to the template :

 userSearch = UserSearch.all()
 us = userSearch.fetch(1000)
 template_values = { 'userSearch' : us }
 path = os.path.join(os.path.dirname(__file__), 'template/index.html')
 self.response.out.write(template.render(path, template_values))

In the template I print the ID in the HTML :

{% for us in userSearch %}
    <tr>
        <td><a href="/delete?id={{ us.ID }}">delete</a></td>
        <td>...</td>
    </tr>
{% endfor %}

But in the HTML the ID is empty, so I cannot delete my item :

class DeleteItem(webapp.RequestHandler):
    def get(self):
        id = int(self.request.get('id'))
        item = UserSearch.get_by_id(id)
        if item != None:
            db.delete(item)

I don't know what is wrong? I don't even know if it's the way to proceed... get an element to delete... Thank you for your help

Was it helpful?

Solution

Assuming 1) you are referring to the built-in id() and 2) you are using Django templating, your template syntax should instead look like this:

{% for us in userSearch %}
    <tr>
        <td><a href="/delete?id={{ us.key.id }}">delete</a></td>
        <td>...</td>
    </tr>
{% endfor %}

If you are using Jinja2 templating the syntax would be slightly different:

{% for us in userSearch %}
    <tr>
        <td><a href="/delete?id={{ us.key().id() }}">delete</a></td>
        <td>...</td>
    </tr>
{% endfor %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top