如何在表格中使用GeoDjango Pointfield?

时间:2014-12-04 23:32:07

标签: django openlayers geodjango

我想知道使用从Django表单自动生成的PointField小部件。

我正在使用此通用视图(CreateView)

这就是我的模型。

from django.contrib.gis.db import models

class Post(models.Model):
    title = models.CharField(max_length=60)
    text = models.CharField(max_length=255)
    location = models.PointField(geography=True, null=True, blank=True)
    objects = models.GeoManager()

然后自动为我生成表单,我只是在我的视图中调用它。就这样:

{{form.as_p}}

这是该段代码的输出。

<form method="post">
  <input type='hidden' name='csrfmiddlewaretoken' value='wVZJIf7098cyREWe3n3jiZinPdbl8nEe' />
  <p><label for="id_title">Title:</label> <input id="id_title" maxlength="60" name="title" type="text" /></p>
<p><label for="id_text">Text:</label> <input id="id_text" maxlength="255" name="text" type="text" /></p>
<p><label for="id_location">Location:</label> <style type="text/css">
    #id_location_map { width: 600px; height: 400px; }
    #id_location_map .aligned label { float: inherit; }
    #id_location_div_map { position: relative; vertical-align: top; float: left; }
    #id_location { display: none; }
    .olControlEditingToolbar .olControlModifyFeatureItemActive {
        background-image: url("/static/admin/img/gis/move_vertex_on.png");
        background-repeat: no-repeat;
    }
    .olControlEditingToolbar .olControlModifyFeatureItemInactive {
        background-image: url("/static/admin/img/gis/move_vertex_off.png");
        background-repeat: no-repeat;
    }
</style>

<div id="id_location_div_map">
    <div id="id_location_map"></div>
    <span class="clear_features"><a href="javascript:geodjango_location.clearFeatures()">Delete all Features</a></span>

    <textarea id="id_location" class="vSerializedField required" cols="150" rows="10" name="location"></textarea>
    <script type="text/javascript">
        var map_options = {};
        var options = {
            geom_name: 'Point',
            id: 'id_location',
            map_id: 'id_location_map',
            map_options: map_options,
            map_srid: 4326,
            name: 'location'
        };

        var geodjango_location = new MapWidget(options);
    </script>
</div>
</p>
  <input type="submit" value="Create" />
</form>

在head标签中,我从中导入OpenLayers脚本     http://cdnjs.cloudflare.com/ajax/libs/openlayers/2.13.1/OpenLayers.js

但是,该页面不会显示任何针对该点的内容。 (其他领域工作得很好)。

在chromedevtools中显示此错误

Uncaught ReferenceError: MapWidget is not defined 

对于这行代码

var geodjango_location = new MapWidget(options)

基本上我想知道是否还有其他javascript库我应该链接到或者我错过了其他的东西?

我查看了GeoDjango表单上的文档,但不知道还有什么可以尝试     https://docs.djangoproject.com/en/dev/ref/contrib/gis/forms-api/

2 个答案:

答案 0 :(得分:11)

将此添加到头部:

<head>
  {{ form.media }}
</head>

答案 1 :(得分:0)

我的Admin UI存在相关问题。我的解决方案只是您问题的参考。 Firefox浏览器阻止加载混合http / https http://openlayers.org/api/2.13/OpenLayers.js,因为我的geodjango网站强制https。

一种解决方案是将OpenLayer.js下载到我的geodjango项目静态目录中,并将以下行添加到我的CustomGeoModelAdmin:

class MyCustomGeoModelAdmin(....):
    openlayers_url = '/static/mygeodjangoproject/OpenLayers.js'

    @property 
    def media(self):
        "Injects OpenLayers JavaScript into the admin."
        media = super(MyCustomGeoModelAdmin, self).media
        media.add_js([self.openlayers_url])
        return media

和voilà,我的管理站点现在显示了Point Field的地理地图。