django-jet icon indicating copy to clipboard operation
django-jet copied to clipboard

OpenLayers.js:679 Uncaught TypeError: Cannot read property 'w' of null(…)

Open psychok7 opened this issue 7 years ago • 13 comments

Hi, i am getting this error when i try to render a Geodjango map in my admin. This works well in a "normal" django admin installation.

To be more specific:

from django.contrib.gis import admin as gis_admin


class SecureOSMGeoAdmin(gis_admin.OSMGeoAdmin):
    openlayers_url = (
        'https://cdnjs.cloudflare.com/ajax/libs/openlayers/2.13.1/OpenLayers.js'
    )

And

class AccommodationAdmin(SecureOSMGeoAdmin):
   pass

admin.site.register(Accommodation, AccommodationAdmin) 

The model:

class Accommodation(models.Model):    
    location = models.PointField(srid=4326, db_index=True, blank=True, null=True)

Any ideas? I am using Django==1.10

psychok7 avatar Nov 15 '16 12:11 psychok7

Any update on this? Same problem here using django 1.10.5 and django-jet 1.0.4, though I am using the admin.ModelAdmin class instead of OSMGeoAdmin.

gonzalomcruz avatar Feb 28 '17 14:02 gonzalomcruz

This only happens when the gis field / fields are not in the first tab. Since Jet renders tabs on demand the gis initializations can only occur when the given tab is open and rendered.

I came up with the a fix for openlayers, extend the template "gis/admin/openlayers.html" with the following code (NOTE: this fix uses code from jet, to check modules hash, and that can change in the future, ideally this should be included in the source code of jet to avoid this issues)

{% extends "gis/admin/openlayers.html" %}

{% block init_function %}jQuery(document).ready(function ($) {
    var $modules = $('.change-form').find('#content-main > form > div').find('> .module'),
        gis_module_classes = $('#{{ id }}_map').parents('.module')[0].classList,
        tab_changed_function = function (hash, module, event) {
            var result = hash.match(/^(#(\/tab\/(.+)\/)?)?$/i);

            if (result == null) {
                return;
            }

            result = result[3] != undefined ? result[3] : '';

            if (result == module) {
                {{ module }}.init();
                if (event) {
                    $(this).off(event);
                } else {
                    return true;
                }
            }
            return false;
        };

    $modules.each(function (i) {
        var module = 'module_' + i;
        if ($.inArray(module, gis_module_classes) != -1) {
            // immediate call, if already in tab
            if (! tab_changed_function(location.hash, module)) {
                $(window).on('hashchange', function (event) {
                   tab_changed_function(location.hash, module, event);
                });
            }
        }
    });
});{% endblock %}

davidmgvaz avatar Jun 19 '17 15:06 davidmgvaz

Please come to the django-jet Discord server so we can organize if you like:

  • https://discord.gg/3yr5yeH

Welcome! 😄

Ismael-VC avatar Aug 15 '17 15:08 Ismael-VC

Hello, I have the same error using django's MultiPolygonField. Any solution?

poly_map = models.MultiPolygonField(null=True, blank=False)

lzambrano18 avatar Jan 18 '18 16:01 lzambrano18

Which is the traceback from this bug?

SalahAdDin avatar Jan 26 '18 19:01 SalahAdDin

I had the same Issue on Django 1.11. I fixed it by modifying template "templates\gis\admin\openlayers.html":

Changed:

<script type="text/javascript">{% block init_function %}{{ module }}.init();{% endblock %}</script>

With:

<script type="text/javascript">{% block init_function %}
jQuery(document).ready(function ($) {    {{ module }}.init();  });
{% endblock %}</script>
 

gslaboch avatar Apr 21 '18 04:04 gslaboch

@gslaboch It is a problem with this package or with django?

SalahAdDin avatar Apr 21 '18 20:04 SalahAdDin

I think its a problem django.contrib.gis, but it only appears when working together with django-jet. The original code just makes {{ module }}.init(); assuming document is fully loaded

gslaboch avatar Apr 25 '18 02:04 gslaboch

@gslaboch can you make a pull request here?

SalahAdDin avatar Apr 25 '18 08:04 SalahAdDin

Any news/fix about this? Django==2.1.3 django-jet==1.0.8 Map still don't work.

tomoki16 avatar Nov 28 '18 13:11 tomoki16

I proposed at Django the change you suggested @gslaboch: https://github.com/django/django/pull/11819

avdata99 avatar Sep 26 '19 01:09 avdata99

@avdata99 as far as i understand, the normally don't make changes related to third party packages, less when it is not a popular package.

SalahAdDin avatar Sep 26 '19 14:09 SalahAdDin

I've tried @gslaboch 's solution but for some unknown reason it is not working.

So kind of used his solution and do a bit of a hack and only then it worked on my end. Here is the code: Screenshot_2

<script type="text/javascript">{% block init_function %}
let geoDelay = 5000;

let geoTimerId = setTimeout(function initGeoMap() {
  if ($('#id_geo_location_admin_map').length) {
    {{ module }}.init();
  console.log('asdf');
  } else {
    geoTimerId = setTimeout(initGeoMap, geoDelay);
  }

}, geoDelay);
{% endblock %}</script>

hanztura avatar Jan 21 '21 21:01 hanztura