将变量从javascript传递到服务器(django)

时间:2021-10-24 16:04:35

I am trying to send the users current location variable (after the user clicks allow) from the browser to Django server using jQuery's post method. The current location is stored in the variable pos.

我试图使用jQuery的post方法将用户当前位置变量(在用户点击允许后)从浏览器发送到Django服务器。当前位置存储在变量pos中。

$(document).ready(function(){
    $.post("/location", pos)
});

In django, i've created a url /location in urls.py which captures the pos variable in views.py through request.POST(pos), which I use to carryout distance lookups.

在django中,我在urls.py中创建了一个url / location,它通过request.POST(pos)捕获views.py中的pos变量,我用它来进行距离查找。

I see that the variable is not being passed to the django server, Can someone please advise where I am going wrong ?

我看到变量没有传递给django服务器,有人可以告诉我哪里出错吗?

1 个解决方案

#1


6  

I have assigned the JavaScript location variable(pos) in Google geolocation API to an input element (id= "location") in HTML form by using the below code

我已使用以下代码将Google地理位置API中的JavaScript位置变量(pos)分配为HTML格式的输入元素(id =“location”)

document.getElementById('location').value = pos

Below is my HTML form

下面是我的HTML表单

<form id = "geolocation" action="/location" method="POST" >
            {% csrf_token %}
        <input type="text" id = "location" name="location" value="" />
        <input type="submit" />

</form>

Then within my google geolocation API, I auto submit the form by adding the below line of code

然后在我的谷歌地理定位API中,我通过添加以下代码行自动提交表单

document.getElementById("geolocation").submit(); 

Finally within Django Views.py file, I use the 'POST' method to obtain the location from the client side within the function where I am using the variable

最后在Django Views.py文件中,我使用'POST'方法从我在使用变量的函数中从客户端获取位置

user_location = request.POST.get('location')

Here is a link to Google Geolocation API.

以下是Google Geolocation API的链接。

I've inserted excerpt from my code, just so that you may know where exactly I've used the above lines of JavaScript code in the Google Geolocation API.

我已经从我的代码中插入了摘录,以便您可以知道我在Google Geolocation API中使用了上述JavaScript代码的确切位置。

var infowindow = new google.maps.InfoWindow({
    map: map,
    position: pos,
    content: 'Location found using HTML5.'
  });
  // the below line has been inserted to assign a JS variable to HTML input field called 'geolocation' 
document.getElementById('location').value = pos
map.setCenter(pos);
// the below line has been inserted to autosubmit the form.  
document.getElementById("geolocation").submit(); 

#1


6  

I have assigned the JavaScript location variable(pos) in Google geolocation API to an input element (id= "location") in HTML form by using the below code

我已使用以下代码将Google地理位置API中的JavaScript位置变量(pos)分配为HTML格式的输入元素(id =“location”)

document.getElementById('location').value = pos

Below is my HTML form

下面是我的HTML表单

<form id = "geolocation" action="/location" method="POST" >
            {% csrf_token %}
        <input type="text" id = "location" name="location" value="" />
        <input type="submit" />

</form>

Then within my google geolocation API, I auto submit the form by adding the below line of code

然后在我的谷歌地理定位API中,我通过添加以下代码行自动提交表单

document.getElementById("geolocation").submit(); 

Finally within Django Views.py file, I use the 'POST' method to obtain the location from the client side within the function where I am using the variable

最后在Django Views.py文件中,我使用'POST'方法从我在使用变量的函数中从客户端获取位置

user_location = request.POST.get('location')

Here is a link to Google Geolocation API.

以下是Google Geolocation API的链接。

I've inserted excerpt from my code, just so that you may know where exactly I've used the above lines of JavaScript code in the Google Geolocation API.

我已经从我的代码中插入了摘录,以便您可以知道我在Google Geolocation API中使用了上述JavaScript代码的确切位置。

var infowindow = new google.maps.InfoWindow({
    map: map,
    position: pos,
    content: 'Location found using HTML5.'
  });
  // the below line has been inserted to assign a JS variable to HTML input field called 'geolocation' 
document.getElementById('location').value = pos
map.setCenter(pos);
// the below line has been inserted to autosubmit the form.  
document.getElementById("geolocation").submit();