我的第一个Google App Engine/Python应用

0 投票
2 回答
1139 浏览
提问于 2025-04-17 19:48

我正在尝试写我的第一个GAE/Python应用程序,想实现以下三个功能:

  1. 显示一个表单,让用户可以输入他们的个人信息(index.html)
  2. 把提交的表单数据存储到数据库中
  3. 从数据库中取出所有数据,并在表单上方显示所有结果(index.html)

但是,我遇到了以下错误:

第15行,在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

编辑 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

好的,到目前为止进展不错。数据已经存储在数据库中了。然而,我的页面却是空白的……

2 个回答

1

在你的app.yaml文件中,它不喜欢下划线。

#application: some_name
application: somename

还有一些其他问题,比如有些代码块没有关闭等等,这些你需要自己去解决。

1

你遇到了缩进问题。你在get方法中的第3行及之后的行应该和前两行保持同样的缩进水平。否则,这些行就不算在方法里,而是属于类的定义部分,这样它们会在类被定义的时候执行——那个时候,people这个变量是无法使用的。

撰写回答