次の 3 つのことを行う最初の GAE/Python アプリケーションを作成しようとしています。
- ユーザーが自分自身に関する詳細を入力できるフォームを表示します (index.html)
- 送信されたフォーム データをデータストアに保存します
- データストアからすべてのデータを取得し、すべての結果をフォーム (index.html) の上に表示します。
ただし、次のエラーが表示されます
15 行目、MainPage 'people' : people NameError: name '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は、Unicodeではなく、intまたはlongでなければなりません
編集 3 main.py
person.birth_year = int(self.request.get('birth_year'))
person.height = int(self.request.get('height'))
このエラーを解決しました:
badvalueerror プロパティは、Unicode ではなく、int または long でなければなりません
わかりました。ここまでは順調です。データ ストア内のデータ ストア。しかし、私のページは空白になります...