【发布时间】:2013-03-20 20:50:57
【问题描述】:
我正在尝试编写我的第一个 GAE/Python 应用程序,它执行以下三件事:
- 显示一个表单,用户可以在其中输入有关自己的详细信息 (index.html)
- 将提交的表单数据存储在数据存储区中
- 从数据存储中检索所有数据并在表单 (index.html) 上方显示所有结果
但是,我收到以下错误
第 15 行,在 MainPage 'people' 中: people NameError: name 'people' is not 定义
任何有关如何解决此问题并让我的应用正常运行的建议将不胜感激!
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' 对象没有属性 'get_match_routes'
app = webapp2.WSGIApplication([('/', MainPage),('/new_person',PeopleStore)], debug=True)
好的,表单显示在浏览器中,但是当我提交数据时,我收到此错误:
BadValueError: 属性birth_year 必须是 int 或 long,而不是 统一码
编辑 3 main.py
person.birth_year = int(self.request.get('birth_year'))
person.height = int(self.request.get('height'))
解决了这个错误:
badvalueerror 属性必须是 int 或 long,而不是 unicode
好的,到目前为止很好。数据存储在数据存储中。但是,我的页面出现空白...
【问题讨论】:
-
Daniel 回答了您的问题,但我刚刚注意到您在示例中设置了两次
birth_year;一次是出生年份,第二次被高度覆盖。 -
+1 哦,谢谢!我将在上面的 EDIT 1 中修复它。
-
缩进错误已解决。但是,我收到另一个错误:AttributeError:“str”对象没有属性“get_match_routes”。这是否与 app.yaml 文件中缺少路由有关?
-
我在处理程序中遗漏了 PeopleStore(参见上面的编辑 2)。
标签: python google-app-engine web-applications google-cloud-datastore jinja2