2009-09-29 37 views
0

使用下面的代码,我的模板加载罚款,直到我从,然后我得到以下错误提交:子类webapp.RequestHandler的没有响应属性

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> 

回答

3

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

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

put叫上webapp.RequestHandlertry to handle PUT request, according to docs一个子类。

+1

*拍打额头* - 有时你看不到树林。是的,应该是这样的。谢谢SilentGhost。 – Phil 2009-09-29 17:08:45

相关问题