Ndb ndb.GeoPtProperty如何在python3 django中的tempalte中打印

问题描述 投票:0回答:1

值已存储在 geo = ndb.GeoPtProperty 中 我想在 django 模板上打印它 但 obj print 不是值

即使我尝试打印 attr

geo.lat # try this also
geo.latitude # try this also

即使是全名也不起作用

python-3.x google-app-engine ndb
1个回答
0
投票

不确定你的意思

obj print not value

以下代码有效

class Geo(ndb.Model):
    created = ndb.DateTimeProperty(auto_now_add=True)
    location = ndb.GeoPtProperty()

    @classmethod
    def create_record(cls, latitude, longitude):
        return cls(location = ndb.GeoPt(latitude, longitude)).put()


    @classmethod
    def get_records(cls):
        return cls.query().fetch()


@app.route("/test_geo/")
def create_geo():
    key = Geo.create_record(52.37, 4.88)
    logger.info(f"key: {key}")

    output = Geo.get_records()
    for a in output:
        logger.info (f"lat:{a.location.lat}, long: {a.location.lon}")
        # The above line gives
        lat:52.37, long: 4.88

    return "done"

© www.soinside.com 2019 - 2024. All rights reserved.