2014-12-04 25 views
3

我想知道如何使用从Django表单自动生成的PointField小部件。如何在表单中使用GeoDjango 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

然而,网页不会显示在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/

回答

10

添加这头部分:

<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 

就万事大吉了,我的管理站点现在显示一个点的地理地图领域。

+0

感谢Ben修复我的法语;-) – 2016-05-23 11:05:34

相关问题