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