如何在表单中使用GeoDjango Pointfield?

6
我想知道如何使用从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>

在头标签中,我从http://cdnjs.cloudflare.com/ajax/libs/openlayers/2.13.1/OpenLayers.js导入了一个OpenLayers脚本。
然而,页面不会显示pointfield的任何内容。(其他字段工作正常)
在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个回答

19

将这段代码添加到头部:

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

0

我在我的管理界面中遇到了一个相关的问题。我的解决方案只是为了参考你的问题。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

然后,我的管理站点现在显示了一个点字段的地理地图。


感谢Ben修复我的法语;-) - Lord Mosuma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接