Search code examples
djangoleafletgisgeodjango

why my added layers are not displayed by leaflet.ajax.js


I am developing a web map app using django, leaflet and postgis. My web app could display the OSM (openstreetmap) as background map but my added layer (from QGIS) and point data model (constructed in geodjango) could not be displayed in the web app. please give me a help. [Ofcourse I can see the text format (as json) in my added layer as follows:

https://i.sstatic.net/qYnWe.jpg

The code of index.html:

<!DOCTYPE html>
<html>
    {% load static %}
    {% load leaflet_tags %}
    <head>
        {% leaflet_js %}
        {% leaflet_css %}
    <title>Service Maps for Tourism</title>
    <style type="text/css">
        #gis {width: 80%; height: 800px;}
        </style>

        <script type="text/javascript" src= "{% static 'dist/leaflet.ajax.js' %}"> </script> 

    </head>
    <body>
<h1> سامانه نقشه گردشگری</h1>
<br/>
<script type="text/javascript">
    function our_layers(map,options){
        var datasets = new L.GeoJSON.AJAX("{% url 'name' %}",{
        });
        datasets.addTo(map);
    }
</script>

{% leaflet_map "gis" callback="window.our_Layers" %}
    </body>
</html>

The views.py is as follows:

from django.shortcuts import render
from django.views.generic import TemplateView
from django.core.serializers import serialize
from django.http import HttpResponse
from .models import Provinces
# Create your views here.
class HomePageView(TemplateView):
    template_name= 'index.html'

def province_dataSets (request):
    provinces= serialize('geojson', Provinces.objects.all()) 
    return HttpResponse(provinces, content_type= 'json')   

The models.py is as follows:

from __future__ import unicode_literals
from django.db import models
from django.contrib.gis.db import models
from django.db.models import Manager

# Create your models here.

class TourismPoints(models.Model):
    name= models.CharField(max_length= 50)
    location= models.PointField(srid= 4326)
    objects= models.Manager()

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural= "Tourism points"    

class Provinces(models.Model):
    name = models.CharField(max_length=50)
    english_name = models.CharField(max_length=40)
    geom = models.MultiPolygonField(srid=4326)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural= "Provinces"  

The urls.py is as follows:

from django.conf.urls import include, url
from .views import HomePageView, province_dataSets

from django.urls import path

urlpatterns = [
path ('', HomePageView.as_view (), name='home'),
path('province_data/', province_dataSets, name='name'),]


Solution

  • I used version 2.7 of Django and fortunately the added layers could be displayed