0

我想寫我的第一個GAE/Python應用程序,做以下三兩件事:我的第一個谷歌應用程序引擎/ Python應用程序

  1. 顯示一個表單,用戶可以在大約自理輸入詳細信息(index.html的)
  2. 存儲在數據存儲提交的表單數據
  3. 檢索形式(的index.html)

但是,所有上述結果來自數據存儲的所有數據並顯示,我收到以下錯誤

line 15, in MainPage 'people' : people NameError: name 'people' is not defined

有關如何解決此問題並讓我的應用程序工作的任何建議將不勝感激!

main.py

import webapp2 
import jinja2 
import os 

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__))) 

class MainPage(webapp2.RequestHandler): 
    def get(self): 

     people_query = Person.all() 
     people = people_query.fetch(10) 

    template_values = { 
     'people': people 
    } 

    template = jinja_environment.get_template('index.html') 
    self.response.out.write(template.render(template_values)) 

# retrieve the submitted form data and store in datastore 
class PeopleStore(webapp2.RequestHandler): 
    def post(self): 
     person = Person() 
     person.first_name = self.request.get('first_name') 
     person.last_name = self.request.get('last_name') 
     person.city = self.request.get('city') 
     person.birth_year = self.request.get('birth_year') 
     person.birth_year = self.request.get('height') 
     person.put()   

# models a person class 
class Person(db.Model): 
    first_name = db.StringProperty() 
    last_name = db.StringProperty() 
    city = db.StringProperty() 
    birth_year = db.IntegerProperty() 
    height = db.IntegerProperty() 


app = webapp2.WSGIApplication([('/', MainPage), 
           ('/new_person')], debug=True) 

的index.html

<html> 
    <body> 
     {% for person in people %} 
      {% if person %} 
       <b>{{ person.first_name }}</b> 
       <b>{{ person.last_name }}</b> 
       <b>{{ person.city }}</b> 
       <b>{{ person.birth_year }}</b> 
       <b>{{ person.height }}</b> 
       <hr></hr> 
      {% else %} 
       No people found   
     {% endfor %} 

     <form action="/new_person" method="post">   
      <div><textarea name="first_name" rows="3" cols="60"></textarea></div> 
      <div><textarea name="last_name" rows="3" cols="60"></textarea></div> 
      <div><textarea name="city" rows="3" cols="60"></textarea></div> 
      <div><textarea name="birth_year" rows="3" cols="60"></textarea></div> 
      <div><textarea name="height" rows="3" cols="60"></textarea></div> 
      <div><input type="submit" value="Submit"></div> 
     </form>   
    </body> 
</html> 

的app.yaml

application: some_name 
version: 1 
runtime: python27 
api_version: 1 
threadsafe: true 

handlers: 
- url: /.* 
    script: main.app 

libraries: 
- name: jinja2 
    version: latest 

EDIT 1 * main.py *

import webapp2 
import jinja2 
import os 

from google.appengine.ext import db 

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__))) 

class MainPage(webapp2.RequestHandler): 
    def get(self): 

     people_query = Person.all() 
     people = people_query.fetch(10) 

     template_values = { 
      'people': people 
     } 

     template = jinja_environment.get_template('index.html') 
     self.response.out.write(template.render(template_values)) 


class PeopleStore(webapp2.RequestHandler): 
    def post(self): 
     person = Person() 
     person.first_name = self.request.get('first_name') 
     person.last_name = self.request.get('last_name') 
     person.city = self.request.get('city') 
     person.birth_year = self.request.get('birth_year') 
     person.height = self.request.get('height') 
     person.put()   


class Person(db.Model): 
    first_name = db.StringProperty() 
    last_name = db.StringProperty() 
    city = db.StringProperty() 
    birth_year = db.IntegerProperty() 
    height = db.IntegerProperty() 


app = webapp2.WSGIApplication([('/', MainPage), 
           ('/new_person')], debug=True) 

編輯2 * main.py *

固定此錯誤

以下編輯

AttributeError: 'str' object has no attribute 'get_match_routes'

app = webapp2.WSGIApplication([('/', MainPage),('/new_person',PeopleStore)], debug=True) 

好大,形式顯示在瀏覽器,但是當我提交了數據,我得到這個錯誤:

BadValueError: Property birth_year must be an int or long, not a unicode


編輯3 main.py

person.birth_year = int(self.request.get('birth_year')) 
person.height = int(self.request.get('height')) 

解決了此錯誤:

badvalueerror property must be an int or long, not a unicode

好吧,目前爲止還不錯。數據存儲在數據存儲中。然而,我的頁面空白...

+1

丹尼爾回答你的問題的工作,但我只是注意到你在樣本中設置了兩次'birth_year';一旦出生那一年,第二次被它的身高覆蓋。 – 2013-03-20 21:08:42

+0

+1 ohhh謝謝!我將在上面的編輯1中修復它。 – Anthony 2013-03-20 21:10:33

+0

縮進錯誤已解決。但是,我收到另一個錯誤:AttributeError:'str'對象沒有屬性'get_match_routes'。這是否與app.yaml文件中缺少路線有關? – Anthony 2013-03-20 21:17:47

回答

1

你有一個縮進問題。您的get方法的第3行及以後的行應與前兩行縮進相同的級別。否則,它們不是方法的一部分,而是類定義本身,並且將在類定義時執行 - 此時在範圍內沒有變量people

+0

+1感謝您的回覆。我解決了縮進問題(參見編輯1)。我得到這個錯誤:AttributeError:'str'object has no attribute'get_match_routes' – Anthony 2013-03-20 21:08:39

+0

我在我的處理程序中忽略了PeopleStore:app = webapp2.WSGIApplication([('/',MainPage), ('/ new_person' )],debug = True)。 – Anthony 2013-03-20 21:32:47

1

在你的app.yaml它不象下劃線

#application: some_name 
application: somename 

有一些更多的問題與塊不關閉等,這需要通過自己

+0

+1非常感謝,我修復了應用程序名稱。 – Anthony 2013-03-20 21:34:01