【问题标题】:Uncaught TypeError: L.GeometryField is not a constructor django-leaflet未捕获的类型错误:L.GeometryField 不是构造函数 django-leaflet
【发布时间】:2021-01-11 18:07:26
【问题描述】:

我正在尝试将地理位置发布到 Django 传单上的 postgres 数据库。我遇到Uncaught TypeError:L.GeometryField is not a constructor。我正在使用ajax

    <script>
      var csrftoken = "{{ csrf_token }}";

      function csrfSafeMethod(method) {
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
      }

      $.ajaxSetup({
        beforeSend: function(xhr, settings) {
          if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
          }
        }
      });

       
  </script>
{% endblock %}
<strong>ያሉበት ቦታ|Location ፡</strong>

<p id="demo" onload="getLocation()"></p>


<form method="post" >
  {% csrf_token %}
  {{ form.as_p }}
</form>
<script type="text/javascript">

      var x=document.getElementById("demo").addEventListener("change",save_data);       

       function getLocation()
         {
        if (navigator.geolocation)
        {
        navigator.geolocation.getCurrentPosition(showPosition);
        }
        else{x.innerHTML="Geolocation is not supported by this browser.";}
        }
        function showPosition(position)
        {
        x.innerHTML="Latitude: " + position.coords.latitude + 
        "<br>Longitude: " + position.coords.longitude;

        }
        function save_data(){
          var token=$("{{ csrf_token }}");
          var pos =$("#demo").val();
          console.log("THIS IS:",x);
          $.ajax({
            type:"POST",
            headers:{"X-CSRFToken":token},
            url: "{% url 'shop:user-create' %}",
            data:{pos:pos},

          });
        } 
      
</script>

错误:

<script type="text/javascript">
    var geodjango_id_user_loc = {};
    geodjango_id_user_loc.fieldid = 'id_user_loc';
    geodjango_id_user_loc.modifiable = true;
    geodjango_id_user_loc.geom_type = 'Point';
    geodjango_id_user_loc.srid = 4326;
    

    function id_user_loc_map_callback(map, options) {
        geodjango_id_user_loc.store_class = L.FieldStore;
        (new L.GeometryField(geodjango_id_user_loc)).addTo(map);
        
    };

    
</script>

【问题讨论】:

    标签: javascript ajax django-leaflet


    【解决方案1】:

    我遇到了同样的问题,通过将plugins="forms" 添加到{% leaflet_js %}{% leaflet_css %} 解决了这个问题

    【讨论】:

      猜你喜欢
      • 2021-08-17
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多