问题
I'm trying to display the OSMWidget in a form using generic CreateVIew template.
# models.py
class Building(models.Model):
point = PointField('kort markør', null=True)
country = models.CharField('land', max_length=100, blank=True, null=True, default='Danmark')
city = models.CharField('by', max_length=100, blank=True, null=True)
# views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic import ListView, CreateView, UpdateView, DeleteView, DetailView
from django.contrib.gis import forms
from .forms import BuildingForm
from .models import Building
class BuildingCreateView(LoginRequiredMixin, CreateView):
form_class = BuildingForm
template_name = 'cms/building_form.html'
# forms.py
from django.contrib.gis.forms import OSMWidget, PointField, ModelForm
from .models import Building
class BuildingForm(ModelForm):
point = PointField(
widget=OSMWidget(
attrs={'map_width': 600,
'map_height': 400,
'template_name': 'gis/openlayers-osm.html',
'default_lat': 57,
'default_lon': 12}))
class Meta:
model = Building
fields = ['city', 'country', 'point']
# buildings_form.html
{% block content %}
<form enctype="multipart/form-data" method="post" action="">
{% csrf_token %}
<ul>
{{ form.as_ul }}
</ul>
<input type="submit" value="Submit"/>
</form>
{% endblock %}
But the map don't show in the template, but it just shows up as an empty div. If I inspect the elements I can see this.
# web inspector
<script type="text/javascript">
var map_options = {};
var base_layer = new ol.layer.Tile({source: new ol.source.OSM()});
var options = {
base_layer: base_layer,
geom_name: 'Point',
id: 'id_point',
map_id: 'id_point_map',
map_options: map_options,
map_srid: 3857,
name: 'point'
};
options['default_lon'] = 12;
options['default_lat'] = 57;
options['default_zoom'] = 12;
var geodjango_point = new MapWidget(options);
</script>
And the console output this error: ReferenceError: ol is not defined
So I am thinking that it doesn't load the javascript. Or that I need to specific where the js lives in the widget attributes. But I can't find anything in the docs.
I have also tried the following:
It loads the resources from cloudflare, but it throws ReferenceError: MapWidget is not defined
# building_form.html
{% block extra_css %}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.20.1/ol.css">
{% endblock %}
{% block extra_js %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.20.1/ol.js"></script>
{% endblock %}
and adding this to the forms media class, but it don't trigger the js and css in the head section of the template and throws the ReferenceError: ol is not defined
class BuildingForm(ModelForm):
point = PointField(
widget=OSMWidget(
attrs={'map_width': 600,
'map_height': 400,
'template_name': 'gis/openlayers-osm.html',
'default_lat': 57,
'default_lon': 12}))
class Meta:
model = Building
fields = ['project', 'description', 'point']
class Media:
css = {
'all': (
'https://cdnjs.cloudflare.com/ajax/libs/ol3/3.20.1/ol.css',
'gis/css/ol3.css',
)
}
js = (
'https://cdnjs.cloudflare.com/ajax/libs/ol3/3.20.1/ol.js',
'gis/js/OLMapWidget.js',
)
But if I test the contents of media in the console everything is fine:
w = BuildingForm()
>>> print(w.media)
<link href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.20.1/ol.css" type="text/css" media="all" rel="stylesheet" />
<link href="/static/gis/css/ol3.css" type="text/css" media="all" rel="stylesheet" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.20.1/ol.js"></script>
<script type="text/javascript" src="/static/gis/js/OLMapWidget.js"></script>
Can anyone help? I'm super confused. Thanks.
回答1:
A couple of changes to be made here:
In buildings_form.html, render media in head section of your template.
<html>
<head>
{{ form.media }}
</head>
<body>
<div id="map">
<form enctype="multipart/form-data" method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit"/>
</form>
</div>
</body>
</html>
When subclassing ModelForm like you have, it is also necessary to set it as the form class in your custom CreateView.
class MapCreateView(CreateView):
form_class = MapForm
template_name = 'buildings_form.html'
Also in custom ModelForm, widgets for fields should be specified in widgets class property.
class BuildingForm(ModelForm):
class Meta:
model = Building
fields = ('point',)
widgets = {
'point': gis_forms.OSMWidget(
attrs={
'map_width': 800,
'map_height': 500,
}
)
}
来源:https://stackoverflow.com/questions/47750850/osmwidget-map-doesnt-show-in-template-referenceerror-ol-is-not-defined