How to use GeoDjango Pointfield in Form?

2020-07-17 04:45发布

问题:

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

I am using the generic views for this (CreateView)

This is what my model looks like.

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

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>

In the head tags I import an OpenLayers script from 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).

In chromedevtools it shows this error

Uncaught ReferenceError: MapWidget is not defined 

For this line of code

var geodjango_location = new MapWidget(options)

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

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/

回答1:

Add this to the head section:

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


回答2:

I have a related problem with my Admin UI. My solution is just a reference for your problem. Firefox browser was blocking the loading of mix http/https http://openlayers.org/api/2.13/OpenLayers.js because my geodjango site forces https.

One solution is to download the OpenLayer.js into my geodjango project static directory, and add the following line to my 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

and voilà, my admin site now shows a Geographical Map for the Point Field.