[英]Communicating from html or javascript to python Django
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.
我正在使用mapbox js框架,并希望将用户位置“位置”发送到我的python视图代码中,以将其存储在数据库中。 When I do a console.log() in the js code it doesnt print anything.
当我在js代码中执行console.log()时,它不会打印任何内容。 When I do a print in python it prints 'None'.
当我在python中进行打印时,它会打印“无”。 I tried ajax like other posts had but nothing seems to work.
我像其他帖子一样尝试过ajax,但似乎没有任何效果。 Maybe I am going about passing data from templates to python code in the wrong way... Please help
也许我打算以错误的方式将数据从模板传递到python代码...请帮助
base.html 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 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 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. 我无法弄清楚ajax jquery语句,但是我发现我可以只使用普通AXAJ来使用XMLHttpRequest发送Post请求。 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.
使用这种方法,我无需提交页面即可从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.
我只需要从Django的“管理”面板中发送身份验证令牌,即可从发送到模板的python变量中获取身份验证令牌。
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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.