views:

344

answers:

1

Using the below code, my template loads fine until I submit the from, then I get the following error:

e = AttributeError("'ToDo' object has no attribute 'response'",)

Why doesn't my ToDo object not have a response attribute? It works the first time it's called.

import cgi
import os 

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db

class Task(db.Model):
    description = db.StringProperty(required=True)
    complete = db.BooleanProperty()

class ToDo(webapp.RequestHandler):

    def get(self):
        todo_query = Task.all()
        todos = todo_query.fetch(10)
        template_values = { 'todos': todos }

        self.renderPage('index.html', template_values)

    def renderPage(self, filename, values):
        path = os.path.join(os.path.dirname(__file__), filename)
        self.response.out.write(template.render(path, values))          


class UpdateList(webapp.RequestHandler):
    def post(self):
        todo = ToDo()
        todo.description = self.request.get('description')
        todo.put()
        self.redirect('/')

application = webapp.WSGIApplication(
                                     [('/', ToDo), 
                                      ('/add', UpdateList)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

Here's the template code so far, I'm just listing the descriptions for now.

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
 <head>
 <title>ToDo tutorial</title>
 </head>
 <body>
 <div>
 {% for todo in todos %}
  <em>{{ todo.description|escape }}</em>
 {% endfor %}
 </div>

 <h3>Add item</h3>
 <form action="/add" method="post">
  <label for="description">Description:</label>
  <input type="text" id="description" name="description" />
  <input type="submit" value="Add Item" />
 </form> 
 </body>
</html>
+2  A: 

why do you do what you do in post? it should be:

def post(self):
    task = Task()                # not ToDo()
    task.description = self.request.get('description')
    task.put()
    self.redirect('/')

put called on a subclass of webapp.RequestHandler will try to handle PUT request, according to docs.

SilentGhost
*slaps forehead* - sometimes you can't see the woods for the trees. Yeah, it should have been that. Thanks SilentGhost.
Phil