My first Google App Engine/Python app
Question: I am trying to write my first GAE/Python application that does the following
three things:

  1. Displays a form where the user can enter details about themself (index.html)
  2. Stores the submitted form data in the datastore
  3. Retrieves all data from datastore and displays all results above the form (index.html)

However, I'm getting the following error

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

Any advice on how to resolve this and get my app working will be appreciated!

**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)
    

* * *

**EDIT 2** *_main.py_ *

The following edit fixed this error

> AttributeError: 'str' object has no attribute 'get_match_routes'
    
    
    app = webapp2.WSGIApplication([('/', MainPage),('/new_person',PeopleStore)], debug=True)
    

Ok great, the form displays in the browser, but when I submit the data, I get
this error:

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

* * *

**EDIT 3 main.py**

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

resolved this error:

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

Ok, good so far. The data stores in the data store. However, my page comes up
blank...


Answer: You have an indentation problem. Lines 3 and onwards of your `get` method
should be indented at the same level as the first two lines. Otherwise, they
are not part of the method, but the class definition itself, and will be
executed when the class is defined - at which point there is no variable
`people` in scope.

