我需要 index.py 上的表单,以及由 guestbook.py 处理的 Datastore 查询响应(此处为“comment”),因此表单响应在表单提交后显示给访问者,因此有 2 页。在与我的应用程序集成之前,我正在编辑 Google 的留言簿以尽可能简单地执行此操作。数据存储正在运行,但响应未显示回访问者,POST 302。
application: guestbook
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /
script: index.app
- url: .*
script: guestbook.app
libraries:
- name: webapp2
version: "2.5.1"
索引.py
#!/usr/bin/env python
import cgi
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write("""<html>
<body>
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</body>
</html>""")
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
留言簿.py
#!/usr/bin/env python
#
import cgi
import datetime
import webapp2
from google.appengine.ext import db
from google.appengine.api import users
class Greeting(db.Model):
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
class MainPage(webapp2.RequestHandler):
def get(self):
content = self.request.get("content")
self.response.out.write('<html><body>')
greetings = db.GqlQuery("SELECT * "
"FROM Greeting "
"ORDER BY date DESC LIMIT 1")
for greeting in greetings:
self.response.out.write('<blockquote>%s</blockquote>' %
cgi.escape(greeting.content))
self.response.out.write('</body></html>')
class Guestbook(webapp2.RequestHandler):
def post(self):
content = self.request.get("content")
greeting = Greeting(content = content)
greeting.put()
self.redirect('/')
app = webapp2.WSGIApplication([
('/', MainPage),
('/sign', Guestbook)
], debug=True)