Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!