如何在django 1.9中渲染jsonresponse到模板

时间:2016-01-22 12:33:33

标签: json django leaflet geojson geodjango

我想在地图上渲染geojson数据,并希望在用户点击标记时显示弹出窗口。以下是views.py

中的代码
syncData()

models.py

def extract_raster_points(request):
    conn = psycopg2.connect(dbname="geodjango",host='localhost',user='postgres',          password='postgres', port=5433)
    cur=conn.cursor()
    dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    res=dict_cur.execute("""SELECT row_to_json(fc) FROM ( SELECT      'FeatureCollection' As type, array_to_json(array_agg(f)) As features FROM (SELECT 'Feature' As type, ST_AsGeoJSON(lg.geom)::json As geometry ,row_to_json((SELECT l FROM (SELECT id, species,rastvalues) As l )) As properties FROM pft_account As lg   ) As f )  As fc;""")

    points=dict_cur.fetchone()
    #datapot = json.loads(str(points))
    datapot = json.loads(json.dumps(points))
    datapotg=datapot[0]
    print(datapotg)
    return JsonResponse(datapotg,safe=False)

urls.py

class Account(models.Model):
    #spid = models.AutoField(primary_key=True)
    species=models.CharField(max_length=255)
    x=models.FloatField()
    y=models.FloatField()

    last_modified = models.DateTimeField(auto_now = True)
    first_created = models.DateTimeField(auto_now_add = True)
    geom = models.PointField(srid=4326)
    rastvalues=models.IntegerField()
    objects=models.GeoManager()


    def __str__(self):
        return "%s %s %s %s" % (self.species, self.geom.x, self.geom.y, self.rastvalues)

pot_pft.html(Javascript代码)

url(r'^potpft.data/$', extract_raster_points, name='extract_raster_points'),
    url(r'^data.data/$', GeoJSONLayerView.as_view(model=Account), name='datapotg'),

如何将jsonresponse呈现给模板,即(pft_pot.html)?

0 个答案:

没有答案