我正在尝试编写我的第一个执行以下三项操作的GAE / Python应用程序:
但是,我收到以下错误
第15行,在MainPage'people'中:人名NameError:名称'people'不是 定义
有关如何解决此问题并使我的应用正常工作的任何建议将不胜感激!
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,而不是a 的unicode
编辑3 main.py
person.birth_year = int(self.request.get('birth_year'))
person.height = int(self.request.get('height'))
解决了这个错误:
badvalueerror属性必须是int或long,而不是unicode
好的,到目前为止还不错。数据存储在数据存储中。但是,我的页面空白了......
答案 0 :(得分:1)
你有缩进问题。 get
方法的第3行和第3行应缩进到与前两行相同的级别。否则,它们不是方法的一部分,而是类定义本身,并且将在定义类时执行 - 此时范围内没有变量people
。
答案 1 :(得分:1)
在你的app.yaml中,它不喜欢下划线
#application: some_name
application: somename
还有一些问题没有关闭等等你需要自己解决的问题