How do I use GeoDjango Pointfield on a form?
I wanted to know to use the PointField widget that is automatically generated from the Django form.
I am using 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 automatically generated for me and I will just name it in my opinion. Thus:
{{form.as_p}}
This is the result of this 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 the OpenLayers script from http://cdnjs.cloudflare.com/ajax/libs/openlayers/2.13.1/OpenLayers.js
However, the page won't show anything for the dot field. (Other fields work fine).
Chromedevtools 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 some other javascript library that I have to link from, or am I missing something else?
I've looked at the GeoDjango forms documentation but don't know what else to try https://docs.djangoproject.com/en/dev/ref/contrib/gis/forms-api/
source to share
I have a problem with my admin interface. My solution is just a link to your problem. Firefox browser was blocking the mix load http / https http://openlayers.org/api/2.13/OpenLayers.js because my geodjango site forces https.
One solution is to load OpenLayer.js into the static directory of the geodjango project 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 geo map for Point Point.
source to share