Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GeoDjango--Python:Can't Display Map

I'm getting troubled (more troubled i guess) in displaying my map (from postgresql database) under Geodjango environment. . i had already successfully imported my shapefile into Posgresql database. Yet, I cannot view my maps whenever I tried to. . i also followed the steps in installing all the libraries needed, django, python and postgresql. .

I am new to Geodjango that's why I cannot trace errors. . and actually i dont nknow how. . maybe i could get help from you guys. .

I always get these errors. . it talks about templates. . i have also my template folder on my project folder. . yet its empty and dont know what it is for. .

can you please help me out on this. ?

more thanks to you and i really appreciate you guys for helping out. .

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/APPLICATION/worldborder/245/

Django Version: 1.6.1
Python Version: 2.7.0 
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'APPLICATION')
Installed Middleware:
 ('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')

Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python27\lib\site-packages\django\contrib\admin\templates\gis\admin\osm.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\auth\templates\gis\admin\osm.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\admin\templates\gis\admin\osm.html (File does not exist)



Template error:
In template C:\Python27\lib\site-packages\django\contrib\admin\templates\admin\includes\fieldset.html, error at line 19
  gis/admin/osm.html
  9 :             {% for field in line %}


  10 :                 <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>


  11 :                     {% if not line.fields|length_is:'1' and not   field.is_readonly %}{{ field.errors }}{% endif %}


  12 :                     {% if field.is_checkbox %}


  13 :                         {{ field.field }}{{ field.label_tag }}


  14 :                     {% else %}


  15 :                         {{ field.label_tag }}


  16 :                         {% if field.is_readonly %}


  17 :                             <p>{{ field.contents|linebreaksbr }}</p>


  18 :                         {% else %}


  19 :                              {{ field.field }} 


 20 :                         {% endif %}


 21 :                     {% endif %}


 22 :                     {% if field.field.help_text %}


 23 :                         <p class="help">{{ field.field.help_text|safe }}</p>


 24 :                     {% endif %}


 25 :                 </div>


 26 :             {% endfor %}


 27 :         </div>


 28 :     {% endfor %}


 29 : </fieldset>


 Traceback:
 File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
 139.                 response = response.render()
 File "C:\Python27\lib\site-packages\django\template\response.py" in render
 105.             self.content = self.rendered_content
 File "C:\Python27\lib\site-packages\django\template\response.py" in rendered_content
 82.         content = template.render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in render
 140.             return self._render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in _render
 134.         return self.nodelist.render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in render
 840.                 bit = self.render_node(node, context)
 File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
 78.             return node.render(context)
 File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
   123.         return compiled_parent._render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in _render
   134.         return self.nodelist.render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in render
   840.                 bit = self.render_node(node, context)
 File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
   78.             return node.render(context)
 File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
   123.         return compiled_parent._render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in _render
   134.         return self.nodelist.render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in render
   840.                 bit = self.render_node(node, context)
 File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
   78.             return node.render(context)
 File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
   62.             result = block.nodelist.render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in render
   840.                 bit = self.render_node(node, context)
 File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
   78.             return node.render(context)
 File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
   62.             result = block.nodelist.render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in render
   840.                 bit = self.render_node(node, context)
 File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
   78.             return node.render(context)
 File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
   196.                         nodelist.append(node.render(context))
 File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
   155.         return self.render_template(self.template, context)
 File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render_template
   137.         output = template.render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in render
   140.             return self._render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in _render
   134.         return self.nodelist.render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in render
   840.                 bit = self.render_node(node, context)
 File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
   78.             return node.render(context)
 File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
   196.                         nodelist.append(node.render(context))
 File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
   196.                         nodelist.append(node.render(context))
 File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
   305.                 return nodelist.render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in render
   840.                 bit = self.render_node(node, context)
 File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
   78.             return node.render(context)
 File "C:\Python27\lib\site-packages\django\template\defaulttags.py" in render
   305.                 return nodelist.render(context)
 File "C:\Python27\lib\site-packages\django\template\base.py" in render
   840.                 bit = self.render_node(node, context)
 File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
   78.             return node.render(context)
 File "C:\Python27\lib\site-packages\django\template\debug.py" in render
   91.             output = force_text(output)
 File "C:\Python27\lib\site-packages\django\utils\encoding.py" in force_text
   100.                 s = s.__unicode__()
 File "C:\Python27\lib\site-packages\django\forms\forms.py" in __str__
   425.         return self.as_widget()
 File "C:\Python27\lib\site-packages\django\forms\forms.py" in as_widget
   475.         return widget.render(name, self.value(), attrs=attrs)
 File "C:\Python27\lib\site-packages\django\contrib\gis\admin\widgets.py" in render
   81.                                        context_instance=geo_context)
 File "C:\Python27\lib\site-packages\django\template\loader.py" in render_to_string
   162.         t = get_template(template_name)
 File "C:\Python27\lib\site-packages\django\template\loader.py" in get_template
   138.     template, origin = find_template(template_name)
 File "C:\Python27\lib\site-packages\django\template\loader.py" in find_template
   131.     raise TemplateDoesNotExist(name)

 Exception Type: TemplateDoesNotExist at /admin/APPLICATION/worldborder/245/
 Exception Value: gis/admin/osm.html
like image 617
king635 Avatar asked Oct 01 '22 15:10

king635


1 Answers

The first map you can create very easily is in your Admin section

admin.py

from django.contrib.gis import admin
from .models import MyLayer

admin.site.register(MyLayer, admin.GeoModelAdmin)

urls.py

from django.conf.urls import patterns, url, include
from django.contrib.gis import admin

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
)

Check that admin app is enable in settings.py.

Then go to your navigator localhost/myapp/admin. You should see a map in your item's description.


Other way: Get your geom data in python, make a GeoJSON out of it and send it to an OpenLayer map:

requires vectorformats and simplejson packages

views.py

#-*- coding: utf-8 -*-
from django.shortcuts import render, redirect
from vectorformats.Formats import Django, GeoJSON

from mysite.models import MyLayer


def geojson(request):
    ly = MyLayer.objects.filter(name='xxx')
    djf = Django.Django(geodjango='mpoly', properties=['name'])
    geoj = GeoJSON.GeoJSON()
    my_geojson = geoj.encode(djf.decode(ly))
    return render(request, "mysite_temp/map.html", {'my_geojson': my_geojson})

In templates, create mysite_temp folder and map.html file. If you write only { {my_geojson} } in map.html you will see the content of the GeoJSON

create OpenLayer map in map.html:

<!DOCTYPE html>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <script src="http://openlayers.org/api/OpenLayers.js"></script> 
    <style>
        html,body {
            height: 99%;
            width: 99%;
        }
        #map {
            width: 100%;
            height: 100%;
            border: 1px solid black;
        }   
    </style>

    <script type="text/javascript">
            var map;

            function init(){
        map = new OpenLayers.Map('map');
                var base_layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
               "http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic'} );
            map.addLayer(base_layer);

        var layer = new OpenLayers.Layer.Vector("layername");
        map.addLayer(layer);

        var format = new OpenLayers.Format.GeoJSON();
        var feat = format.read({{my_geojson|safe}});  
        layer.addFeatures(feat);

        map.setCenter(new OpenLayers.LonLat(l_val,L_val),zoom_val);
        map.addControl(new OpenLayers.Control.LayerSwitcher());
       }
        </script>
    </head>
    <body onload = "init()">
    <div id="map"></div>
    </body>
</html>

Cheers!

like image 135
jcs Avatar answered Oct 05 '22 23:10

jcs