Sunil Kr. Yadav
Sunil Kr. Yadav

Reputation: 283

How to send json from django view and receive it on HTML

I am working on GIS project using GeoDjango and for this I have to render a map using OpenLayers. My snippet accepts JSON to create layers in OpenLayers. I want to send my "Administrative" model of GeoDjango as JSON to my HTML page and receive it on HTML as JSON to render my map.

Please give another suggestion also about how to render my GeoDjango Model in an OpenLayers map.

My adminstrative model:

class UPAdministrative(models.Model):
    name=models.CharField(max_length=51)
    admin_leve=models.CharField(max_length=5)   
    ls=models.LineStringField()
    objects=models.GeoManager()

    def __unicode__(self):
        return self.name

Upvotes: 2

Views: 542

Answers (2)

tcarobruce
tcarobruce

Reputation: 3838

You can serialize your model to a format supported by OpenLayers in a view. For example, this view sends just the geometry as geojson:

from django.http import HttpResponse
from models import UPAdministrative

def upadministrative_geometry_json(request, upadmin_id):
    up_admin = UPAdministrative.objects.get(pk=upadmin_id)
    geojson = up_admin.ls.geojson
    return HttpResponse(geojson, mimetype='application/json')

The question Rendering spatial data of GeoQuerySet in a custom view on GeoDjango has a more detailed example of how to integrate data with OpenLayers.

Upvotes: 0

kisileno
kisileno

Reputation: 787

I think you have to implement CRUD to your models and use it in html. For displaying geoobject I only used django admin page. To write a CRUD see

tastypie

Django REST framework

Upvotes: 1

Related Questions