webapp.RequestHandler的子类没有响应属性

时间:2009-09-29 15:58:17

标签: python google-app-engine post-redirect-get

使用下面的代码,我的模板加载正常,直到我提交from,然后我收到以下错误:

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

为什么我的ToDo对象没有response属性?它第一次被调用时起作用。

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()  

到目前为止,这是模板代码,我现在只列出描述。

<!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>

1 个答案:

答案 0 :(得分:3)

你为什么要在post做你做的事情?它应该是:

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

put调用了webapp.RequestHandler的子类try to handle PUT request, according to docs