I have read through all the postings and cannot find anything that will work for my program. I am using the mapbox js framework and want to send the users location 'position' to my python view code to store it in a database. When I do a console.log() in the js code it doesnt print anything. When I do a print in python it prints 'None'. I tried ajax like other posts had but nothing seems to work. Maybe I am going about passing data from templates to python code in the wrong way... Please help
base.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script>
</head>
</html>
home.html
{% extends "geotracker/base.html" %}
{% load static %}
{% block content %}
<body>
<div id='map' width='100%' style='height:400px'></div>
<form id="myform" name="myform">
{% csrf_token %}
<input type="hidden" id="location" name="location"/>
</form>
<script>
mapboxgl.accessToken = 'mytoken';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/streets-v9',
center: [-96, 37.8], // starting position
zoom: 3 // starting zoom
});
// Add geolocate control to the map.
var geolocate = new mapboxgl.GeolocateControl();
map.addControl(geolocate);
geolocate.on('geolocate', function(e) {
console.log('locating...');
var lon = e.coords.longitude;
var lat = e.coords.latitude
var position = [lon, lat];
var element = document.getElementById('location');
element.innerHTML = position;
$('#myform').submit(function (ev) {
//ev.preventDefault();
console.log('hello');
});
});
</script>
</body>
{% endblock %}
views.py
def home(request):
context = {
"mclient": mclient
}
print("POST in view:\t%s" % request.POST.get('location'))
# get users location if authenticated
if request.user.is_authenticated:
Fan.objects(user=str(request.user)).modify(set__currentPosition=
[0,0], image='http://localhost:8000/media/%s' %
request.user.profile.image, upsert=True)
return render(request, 'geotracker/home.html', context)
I could not figure out the ajax jquery statement but I figured out that I can just use plain AXAJ to send a Post request using XMLHttpRequest. Using this method I was able to submit a form with a post request without refreshing the page to get the users location from the Mapbox Api. I just have to send my auth token from Django's Admin panel which I can get from a python variable sent to my template.
geolocate.on('geolocate', function(e) {
console.log('locating...');
var lon = e.coords.longitude;
var lat = e.coords.latitude
var position = lon + " " + lat;
var element = document.getElementById('location');
element.value = position;
var url = 'http://localhost:8000'
var params = JSON.stringify({
longitude: lon,
latitude: lat
});
// Create a new AJAX request object
var request = new XMLHttpRequest();
// Open a connection to the server
request.open('POST', url, true);
// Actually send the request
var csrftoken = '{{ csrf_token }}'
request.setRequestHeader("Content-type", "application/json");
request.setRequestHeader('X-CSRFToken', csrftoken);
request.send(params);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.