2009-09-29 38 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

相關問題