如何在窗体中使用GeoDjango Pointfield? [英] How to use GeoDjango Pointfield in Form?

查看:134
本文介绍了如何在窗体中使用GeoDjango Pointfield?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to know to to use the PointField widget that is automatically generated from a Django form.

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

I am using the generic views for this (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()

然后,表单自动为我生成,我只是在我的看法中调用它。因此:

The form is then automatically generated for me and I just call it in my view. As such:

{{form.as_p}}

{{ form.as_p }}

这是这段代码的输出。

This is the output of that piece of code.

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

在标题中,我从
导入一个OpenLayers脚本 http://cdnjs.cloudflare.com/ajax/libs/openlayers/2.13.1/OpenLayers。 js

但是,该页面不会显示任何点字段。 (其他字段工作正常)。

However, the page will not show anything for the pointfield. (The other fields work just fine).

在chromedevtools中显示此错误

In chromedevtools it shows this error

Uncaught ReferenceError: MapWidget is not defined 

对于这一行代码

var geodjango_location = new MapWidget(options)

基本上我想知道如果有一些其他的javascript库,我应该链接到或我还缺少别的东西?

Basically I want to know if there is someother javascript library I should be linking to or am I missing something else?

我已经看过关于GeoDjango表单的文档,但不知道还有什么要尝试
https://docs.djangoproject.com/en/dev/ref/contrib/gis/forms-api/

I've looked through the documentation on GeoDjango forms, but don't know what else to try https://docs.djangoproject.com/en/dev/ref/contrib/gis/forms-api/

推荐答案

将此添加到头部:

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

这篇关于如何在窗体中使用GeoDjango Pointfield?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆