Search code examples
python-3.xgoogle-app-enginendb

Ndb ndb.GeoPtProperty how to print in tempalte in python3 django


Value has been store in geo = ndb.GeoPtProperty and i want to print this on django template but obj print not value

even i have try to print attr

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

this is also not working even full name


Solution

  • Not sure what you mean by obj print not value.

    The following code works

    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"