Skip to main content
 首页 » 编程设计

python之如何查询 Google App Engine 数据存储并将结果传递到新页面

2025年05月04日21飞鱼

鉴于以下来自 Google App Engine 的 Python 代码摘录演示了我的工作数据存储模型,我如何查询我的模型实体并在名为 showall.html 和 showlist 的新页面上显示结果.html?

from google.appengine.ext import db 
from google.appengine.ext import webapp 
from google.appengine.ext.webapp.util import run_wsgi_app 
 
class PersonInfo(db.Model): 
    firstname = db.StringProperty() 
 
    lastname = db.StringProperty() 
 
    book = db.StringProperty() 
 
    email = db.StringProperty() 
 
    phone = db.StringProperty() 
 
    os = db.StringProperty() 
 
class MainPage(webapp.RequestHandler): 
    def get(self):   

[...提交表单处理程序...]

class Register(webapp.RequestHandler): 
    def post(self): 
        personinfo = PersonInfo() 
        firstname = self.request.get('firstname') 
        lastname = self.request.get('lastname') 
        phone = self.request.get('phone') 
        book = self.request.get('book') 
        email = self.request.get('email') 
        os = self.request.get('os') 
 
        #This puts stuff in database 
        personinfo.firstname=firstname 
        personinfo.lastname=lastname 
        personinfo.phone=phone 
        personinfo.book=book 
        personinfo.email=email 
        personinfo.os=os  
        personinfo.put() 
 
 
        self.response.out.write(""" 
    <ul> 
      <li><a href = "showall.html">Show All People Registered</a></li> 
      <li><a href = "showlist.html">Show People Inside This List</a></li> 
    </ul>""") 
 
application = webapp.WSGIApplication( 
[('/', MainPage), 
('/sign', Register), 
    ],debug=True) 
 
def main(): 
   run_wsgi_app(application) 
 
 
if __name__ == "__main__": 
    main() 

请您参考如下方法:

来自 Queries and Indexes :

# The Query interface constructs a query using instance methods. 
q = Person.all() 
q.filter("last_name =", "Smith") 
q.filter("height <", 72) 
q.order("-height") 
 
# The GqlQuery interface constructs a query using a GQL query string. 
q = db.GqlQuery("SELECT * FROM Person " + 
                "WHERE last_name = :1 AND height < :2 " + 
                "ORDER BY height DESC", 
                "Smith", 72) 
 
# The query is not executed until results are accessed. 
results = q.fetch(5) 
for p in results: 
  print "%s %s, %d inches tall" % (p.first_name, p.last_name, p.height)