I'm attempting to create a GIS application to display some data onto a OSM map.

I followed the GeoDjango tutorial to get shapefile WorldBorders data into my Postgis Database, but I am unsure on how I could pull the relevant data into a template for use with OpenLayers.

Any advice on how I may be able to connect this data with OpenLayers? I've looked around but can't find anything concrete on how to do this, asides from using GeoServer but I would rather stick with my current stack if possible.

  • which geodjango tutorial did you follow? can you post the link
    – ziggy
    Feb 18, 2017 at 17:11
  • 1
    Please edit the question in response to requests for clarification. It's not fair to those who would answer your question to need to mine comments for critical information.
    – Vince
    Feb 18, 2017 at 17:17

1 Answer 1


I believe this answer will help you: https://stackoverflow.com/questions/3034482/rendering-spatial-data-of-geoqueryset-in-a-custom-view-on-geodjango?answertab=votes#tab-top

Consuming Data

OpenLayers has 'format' objects which can read data: There are formats for GeoJSON and KML, as well as others.

You can load the data using standard XMLHttpRequest mechanisms then parse them with a format:

var f = new OpenLayers.Format.GeoJSON(); var features = f.read(req.responseText); layer.addFeatures(features);

Alternatively, you can use the built in Protocol support to load remote data:

 map = new OpenLayers.Map('map');
 var wms = new OpenLayers.Layer.WMS(
     "OpenLayers WMS", "http://labs.metacarta.com/wms/vmap0",
     {layers: 'basic'}

 var layer = new OpenLayers.Layer.Vector("GML", {
     strategies: [new OpenLayers.Strategy.Fixed()],
     protocol: new OpenLayers.Protocol.HTTP({
        url: "/django/view/json/",
        format: new OpenLayers.Format.GeoJSON()

 map.addLayers([wms, layer]);
 map.zoomToExtent(new OpenLayers.Bounds(
     -3.92, 44.34, 4.87, 49.55

You can see in this example, that the 'url' points to your Django view; all the loading of data and parsing it according to the provided format is included. (You can see a similar example in the OpenLayers example for fixed behavior/http protocol.) Putting it Together

Create a Django view, using vectorformats to return your data as GeoJSON
Create a separate view, which returns an HTML page like the OpenLayers example linked, with the modifications shown in the code

sample. That view serves the HTML page that loads your GeoJSON data and parses it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.