Home > Back-end >  how to send longitude and latitude to javascript script from def django
how to send longitude and latitude to javascript script from def django

Time:04-28

I can't figure out how to replace digital longitude and latitude values with dynamic fields from postgres in javascript how to get the longitude and latitude of a particular place in this script?

<div id='map' style='width: 100%; height: 400px;'></div>
    <script>
    mapboxgl.accessToken = '{{ mapbox_access_token }}';
    var map = new mapboxgl.Map({
      container: 'map',
      style: 'mapbox://styles/mapbox/streets-v10',
      center: [20.890438, 52.256002],
      // {{venue.lat}}, {{venue.long}} How to pass fields in a similar way as all other django fields
      zoom: 9
    });
    
    var marker= new mapboxgl.Marker()
      .setLngLat([20.890438, 52.256002])
      // {{venue.lat}}, {{venue.long}}
      .addTo(map)
    </script>

All rendering functions in django. Here's how to send a specific longitude and latitude for a specific event.

def show_venue(request, venue_id):
venue = Venue.objects.get(pk=venue_id)
venue_owner = User.objects.get(pk=venue.owner)
long = Venue.objects.get(pk=venue.long)
lat = Venue.objects.get(pk=venue.lat)
#addresses = Venue.objects.all()
mapbox_access_token = 'pk.eyJ1IjoicGF2ZWxzdHJ5a2hhciIsImEiOiJjbDI2ZmJmcTkyajBwM2lscDd6aDVwOG4xIn0.15o8i7eD1qwyQcnsx2iBmg'
return render(request, 
    'events/show_venue.html', {
    'venue': venue,
    'venue_owner':venue_owner,
    #'addresses': addresses,
    'mapbox_access_token': mapbox_access_token,
    'lat':lat,
    'long':long,
    })

I will be very grateful for your help

CodePudding user response:

If the problem is the format, you can try this:

center: [{{venue.lat|floatformat:”5”}}, {{venue.long|floatformat:”5”}}]

CodePudding user response:

In Django i would write like this

{{venue.long}} {{venue.lat}}

and in JavaScript it should be like this

['{{venue.long}}','{{venue.lat}}']
  • Related