Search code examples
pythontornado

Python web development dynamic field id on page within Tornado


I am in the process of revisiting Python web development and I am building out a simple CRUD application that sits on top of a Neo4J database. In the UI I do the following

delete.html

<Form method="post">  
   {% for book in books %}  
      {{ book.title }} 
      <input type=hidden name="id" value="{{ book.id}}" />  
      <input type=submit value="Delete" />  
      <br>  
   {% end %}  
</Form> 

Python code:

class DeleteHandler(tornado.web.RequestHandler):  
    def post(self):  
      bookToDelete = self.get_argument("id")  

The issue I have is that it will only take the most recent "id" I realize that this is because the variable is overwritten on each pass of the loop. My question is how do I store the "id" on the page for each book that is to potentially be deleted.


Solution

  • I think the easiest solution in your case is to move the <form> inside of the {% for book in books %} loop, so that each delete button is actually a separate form, giving you only the id of the book you actually want to delete.