gpt4 book ai didi

python - GeoDjango--Python :Can't Display Map

转载 作者:太空宇宙 更新时间:2023-11-03 15:15:14 25 4
gpt4 key购买 nike

我在 Geodjango 环境下显示我的 map (来自 postgresql 数据库)时遇到了麻烦(我想更麻烦)。 .我已经成功地将我的 shapefile 导入到 Posgresql 数据库中。然而,无论何时我都无法查看我的 map 。 .我还按照步骤安装了所有需要的库,django、python 和 postgresql。 .

我是 Geodjango 的新手,这就是我无法追踪错误的原因。 .实际上我不知道怎么做。 .也许我可以得到你们的帮助。 .

我总是遇到这些错误。 .它谈论模板。 .我的项目文件夹中也有我的模板文件夹。 .但它是空的,不知道它是干什么用的。 .

你能帮我解决这个问题吗? ?

非常感谢你们,我非常感谢你们的帮助。 .

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

最佳答案

您可以轻松创建的第一张 map 位于您的管理部分

管理员.py

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

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

网址.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)),
)

检查是否在 settings.py 中启用了管理应用。

然后转到您的导航器 localhost/myapp/admin。您应该会在项目的描述中看到一张 map 。


其他方式:在 python 中获取您的 geom 数据,从中生成 GeoJSON 并将其发送到 OpenLayer map :

需要 vectorformats 和 simplejson 包

View .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})

在模板中,创建 mysite_temp 文件夹和 map.html 文件。如果您只在 map.html 中编写 { {my_geojson} },您将看到 GeoJSON 的内容

在 map.html 中创建 OpenLayer map :

<!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>

干杯!

关于python - GeoDjango--Python :Can't Display Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21671642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com