Google App Engine数据库未显示

时间:2013-10-12 16:51:27

标签: python google-app-engine google-cloud-datastore

我在我的应用程序中创建了一个实体'FlipKart',并使用了一个处理程序来运行查询,以便我可以在HTML表单中显示给定的条目。我有Jinja2模板来组织我的html文件。

以下是该实体的代码:

class FlipKart(db.Model):
    name = db.StringProperty()            
    quantity = db.IntegerProperty()
    total = db.IntegerProperty()    

这是我处理和运行查询的处理程序:

class WebeHandler(Handler):         
    def get(self):
        self.render("we.html")
    def post(self):
        b_name = self.request.get("bkname")
        b_quantity = self.request.get("qnty")
        b_total = self.request.get("tot")        

        if(b_name and b_quantity and b_total):
            a = FlipKart(BookName = b_name, Quantity = b_quantity, GrandTotal = b_total)
            a.put()
            self.final()
    def final(self):
        flips = db.GqlQuery("SELECT * FROM FlipKart")
        self.render("we.html", flips = flips)

这是我的html文件:

<html>
<head>
    <title>Web Engineering</title>
    <style type = "text/css">

    </style>
</head>

<body>
    <form method = "post">
        Book Name<input type = "text" name = "bkname">
        <br>
        Quantity<input type = "text" name = "qnty">
        <br>
        Total Prize<input type = "number" name = "tot">
        <br>
        <input type = "submit">
        <br><br><br>
        <hr>

        {%
            for flip in flips
        %}
            <div>You have ordered a {{flip.name}}</div>
        {%endfor%}
    </form>
</body>

但是,当我运行此应用程序时,我在浏览器中的表单后面得到以下文本:

You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None
You have ordered a None

什么似乎是问题?

2 个答案:

答案 0 :(得分:5)

您的查询没有问题,您可以看到循环重复。 您的问题是您创建实体的代码

a = FlipKart(BookName = b_name, Quantity = b_quantity, GrandTotal = b_total)

您的模型定义为

class FlipKart(db.Model):
    name = db.StringProperty()            
    quantity = db.IntegerProperty()
    total = db.IntegerProperty()   

您实际上并未在实体中存储任何值。这是非常基本的python。

类构造函数调用应该是

a = FlipKart(name = b_name, quantity = b_quantity, total = b_total)

你需要进行int转换以及其他答案,你没有得到赋值错误的唯一原因是你从未将值赋给属性。

另请注意,您的查询可能无法显示由于最终的一致性而添加的最新实体(我会让您对此进行一些阅读)

答案 1 :(得分:0)

我只是想指出,您可能希望重构您的实体,以使您能够进行强一致性查询,因为即使在进行了已经指出的更正后,您将继续获得新增的实体

这是由于最终的一致性。请查看:https://developers.google.com/appengine/docs/python/datastore/structuring_for_strong_consistency