Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
Get value from span to Django view

I have this javascript that gets longitude and latitude from a Google Map:

<script>
    // Get element references
    var confirmBtn = document.getElementById('confirmPosition');
    var onClickPositionView = document.getElementById('onClickPositionView');
    var latonIdlePositionView = document.getElementById('latOnIdlePositionView');
    var longonIdlePositionView = document.getElementById('longOnIdlePositionView');
  
    // Initialize locationPicker plugin
    var lp = new locationPicker('map', {
      setCurrentPosition: true, // You can omit this, defaults to true
    }, {
      zoom: 15 // You can set any google map options here, zoom defaults to 15
    });
    // Listen to map idle event, listening to idle event more accurate than listening to ondrag event
    google.maps.event.addListener(lp.map, 'idle', function (event) {
      // Get current location and show it in HTML
      var location = lp.getMarkerPosition();
      latOnIdlePositionView.innerHTML = location.lat;
      longOnIdlePositionView.innerHTML = location.lng;
    });
  </script>

This is the HTML I have for a user to define his or her location:

<form method="POST" action="{% url 'geolocate' %}">
{% csrf_token %}
    <div class="card-header">
        <div class="float-start">
        <h3 class="card-title">Me localiser</h3>
        </div>
    </div>
    <div class="card-body no-padding">
        <div id="map"></div>
        <p>Latitude: <span id="latOnIdlePositionView"></span> Longitude: <span id="longOnIdlePositionView" ></span></p>
    </div>
    <div class="card-footer text-end">
        <button type="submit" class="btn btn-primary">Mettre à jour</button>
    </div>
</form>

This is my view:

def geolocate(request):
    if request.method == 'POST':
        lat = request.POST.get('lat_kw')
        long = request.POST.get('long_kw')

        caccount = Account.objects.get(username=request.user.username)
        caccount.lat = lat
        caccount.long = long
        caccount.save()
        return redirect('monprofil')

What I want to do is to get the value form the spans latOnIdlePositionView and longOnIdlePositionView and put them in my view.

I know how to do this if I have data in an input field (using name), but I do not know how to do this using spans.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

according to the comment by @raphael

Add this inside form tag in HTML

<input type='hidden' name='lat_kw' id='lat_kw' value=''>
<input type='hidden' name='long_kw' id='long_kw' value=''>

Javascript

document.getElementById('lat_kw').value = location.lat;
document.getElementById('long_kw').value = location.lng;

Then you good to go

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda