我的第一个Google应用程序引擎/Python应用程序
我正在尝试编写我的第一个GAE/Python应用程序,它完成以下三件事:我的第一个Google应用程序引擎/Python应用程序,python,google-app-engine,web-applications,google-cloud-datastore,jinja2,Python,Google App Engine,Web Applications,Google Cloud Datastore,Jinja2,我正在尝试编写我的第一个GAE/Python应用程序,它完成以下三件事: 显示一个表单,用户可以在其中输入自己的详细信息(index.html) 将提交的表单数据存储在数据存储中 从数据存储中检索所有数据,并在表单上方显示所有结果(index.html) 但是,我得到了以下错误 主页“people”中的第15行:人员名称错误:名称“people”不是 明确的 任何关于如何解决此问题并使我的应用程序工作的建议都将不胜感激 main.py import webapp2 import jinja2 i
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)
person.birth_year = int(self.request.get('birth_year'))
person.height = int(self.request.get('height'))
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>
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*
编辑2 *main.py* 以下编辑修复了此错误 AttributeError:“str”对象没有属性“get\u match\u routes” 好的,很好,表单显示在浏览器中,但当我提交数据时,出现以下错误: BadValueError:属性出生年份必须为int或long,而不是 统一码
编辑3 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)
person.birth_year = int(self.request.get('birth_year'))
person.height = int(self.request.get('height'))
已解决此错误:
badvalueerror属性必须是int或long,而不是unicode
好的,到目前为止还不错。数据存储在数据存储中。但是,我的页面显示为空白…您有缩进问题。
get
方法的第3行及以后的行应与前两行缩进相同级别。否则,它们不是方法的一部分,而是类定义本身,并将在定义类时执行-此时,范围内没有变量人员
。在您的应用程序中。yaml它不喜欢下划线
#application: some_name
application: somename
还有一些块未关闭等问题需要您自己解决+1谢谢您的回复。我修复了缩进问题(请参见编辑1)。现在我得到了这个错误:AttributeError:'str'对象没有属性'get_match_routes',我在处理程序中遗漏了PeopleStore:app=webapp2.WSGIApplication([('/',主页),('/new_person')],debug=True)。Daniel回答了你的问题,但我刚刚注意到您在样本中设置了两次
出生年份
;一次是出生年份,第二次被高度覆盖。+1哦,谢谢!我将在上面的编辑1中修复它。缩进错误已解决。但是,我得到了另一个错误:AttributeError:“str”对象没有“get\u match\u routes”属性。这与app.yaml文件中丢失的路由有关吗?我在处理程序中遗漏了PeopleStore(参见上面的编辑2)。