Step 11: Item Form Pathway

There is a form in the template for creating items. There is a method in the CreateItem handler for handling HTTP POST, and creating an item.

I'll need to GET an Item form, making sure it has the list key. Again, CreateItem.get, unchanged:

class CreateItem(webapp.RequestHandler): def get(self, list_key): template_values = { 'item_form': 1, 'list_key': list_key } self.response.out.write(render(template_values))

... and its matching template section:

{% if item_form %} {% ifequal list.key list_key %} <a name="form"></a> <form action="/create_item/" method="post"> <span style="padding-left:15px"> <textarea style="background:#eeee00" name="name" rows=1 cols=33> </textarea><br> </span> <span style="padding-left:15px"> <input type="Submit" name="button" value="Create item"> </span> <span style="padding-left:130px"> <a href="/">cancel</a> </span> <br> <input type="hidden" name="list_key" value="{{ list_key }}"> </form> {% else %} <br> <span style="padding-left:15px"> <a href="/item_form/{{ list.key }}/#form"> Create an item </a> </span> <br><br> {% endifequal %} {% else %}

We'll instantiate Items in the CreateItem.post handler:

def post(self): item = Item() item.list_key = self.request.get('list_key') item.name = self.request.get('name') item.put() self.redirect("/")