OSMWidget-映射未显示在模板中-ReferenceError:未定义ol [英] OSMWidget - map doesn't show in template - ReferenceError: ol is not defined

查看:78
本文介绍了OSMWidget-映射未显示在模板中-ReferenceError:未定义ol的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用通用的 CreateVIew 模板在表单中显示 OSMWidget

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)



< hr>


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








但是地图未显示在模板中,而是仅显示为空div。如果我检查了元素,则可以看到。


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>

控制台输出以下错误: ReferenceError:未定义ol

And the console output this error: ReferenceError: ol is not defined

所以我认为它不会加载JavaScript。或者,我需要具体说明js在小部件属性中的位置。但是我在文档中找不到任何内容。

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.

我也尝试了以下操作:
它从cloudflare加载资源,但抛出 ReferenceError:未定义MapWidget

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

并将其添加到forms媒体类中,但是它不会不会触发模板头部的js和css并引发 ReferenceError:未定义ol

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.

推荐答案

在此处进行以下更改:

buildings_form.html ,在模板的头部渲染媒体。

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>

像您一样对 ModelForm 进行子类化时,它还需要将其设置为自定义CreateView中的表单类。

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'

在自定义ModelForm中,字段控件应该在widgets类属性中指定。

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

这篇关于OSMWidget-映射未显示在模板中-ReferenceError:未定义ol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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