1

I tried to retrieve the polygon which I saved into postgis database. (all polygons) Here is my code:

models.py

from django.contrib.gis.db import models

class MyMap(models.Model):
    name = models.CharField(max_length=50, blank=True, null=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    geom = models.GeometryCollectionField(srid=4326, null=True)
    objects = models.GeoManager()

    def __unicode__(self):
        return self.name

forms.py

from django.contrib.gis import forms
from .models import MyMap

class MyMapForm(forms.ModelForm):

    class Meta:
        model = MyMap
        fields = ['geom']

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .forms import MyMapForm
from .models import MyMap

def map(request):
    map_info = MyMap.objects.all()

    map_data = {
        "map_detail": map_info
    }

    return render(request, 'mymap.html', map_data)


def ajax(request):
    if request.POST.has_key('geom'):
        form = MyMapForm(request.POST or None)
        ...

mymap.html

<form action="" method="POST"> 
    {%csrf_token%}
    <div id="map_canvas"></div> // I use google map api
</form>

<div>
    {% for details in map_detail %}
        {{detail.geom}}
    {% endfor %}
</div>

I don't know where is my problem which I can not show the polygons in the html file !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.