我正在尝试编写我的第一个 GAE/Python 应用程序,它执行以下三件事:
- 显示一个表单,用户可以在其中输入有关自己的详细信息 (index.html)
 - 将提交的表单数据存储在数据存储区中
 - 从数据存储中检索所有数据并在表单上方显示所有结果 (index.html)
 
但是,我收到以下错误
第 15 行,在 MainPage 'people' 中: people NameError: name 'people' is not defined
任何有关如何解决此问题并使我的应用程序正常工作的建议将不胜感激!
主文件
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)
索引.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>
应用程序.yaml
application: some_name
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
  script: main.app
libraries:
- name: jinja2
  version: latest
编辑 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”对象没有属性“get_match_routes”
app = webapp2.WSGIApplication([('/', MainPage),('/new_person',PeopleStore)], debug=True)
好的,表单显示在浏览器中,但是当我提交数据时,我收到此错误:
BadValueError:属性birth_year 必须是 int 或 long,而不是 unicode
编辑 3 main.py
person.birth_year = int(self.request.get('birth_year'))
person.height = int(self.request.get('height'))
解决了这个错误:
badvalueerror 属性必须是 int 或 long,而不是 unicode
好的,到目前为止很好。数据存储在数据存储中。但是,我的页面出现空白...