how to send user latitude and longitude to django view











up vote
-1
down vote

favorite
1












This is the javascript code to get users latitude and longitude.



<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}

}

function showPosition(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
x.innerHTML = "Latitude: " +lat +
"<br>Longitude: " +long ;
}
</script>

</body>
</html>


through that code, I can display the user's latitude and longitude in the front end, but I need to send the lat and long to Django views.py to show the nearby local business. how can i do that?



i've went through this solution but i'm unable to do that. can anyone help me?



EDIT



i have added ajax to code still not able to send lat value to the views.py



    <!DOCTYPE html>
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<p>Click the button to get your coordinates.</p>

<button id="send-my-url-to-django-button" onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}

}

function showPosition(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
x.innerHTML = "Latitude: " +lat +
"<br>Longitude: " +long ;



$(document).on('click',function() {
var lat1 = lat;
var long1 = long;
$("#send-my-url-to-django-button").click(function() {
$.ajax({
url: '{% url "accounts:location" %}',
//type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: {
'lat': lat1,
'long': long1,
//'csrfmiddlewaretoken': '{{ csrf_token }}'
},
success : function(json) {
alert("Successfully sent the URL to Django");
},
error : function(xhr,errmsg,err) {
alert("Could not send URL to Django. Error: " + xhr.status + ": " + xhr.responseText);
}
});
});
});

}
</script>
</body>
</html


after clicking 3 times, i receive lat ang long values in views.py and in browser it shows




Could not send URL to django Error:200;











share|improve this question
























  • Have you tried HTTP? You know, that thing between your server and your browser.
    – sjahan
    Nov 11 at 8:16










  • HTTP means send data using POST and GET methods?
    – Prince Nihith
    Nov 11 at 8:32















up vote
-1
down vote

favorite
1












This is the javascript code to get users latitude and longitude.



<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}

}

function showPosition(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
x.innerHTML = "Latitude: " +lat +
"<br>Longitude: " +long ;
}
</script>

</body>
</html>


through that code, I can display the user's latitude and longitude in the front end, but I need to send the lat and long to Django views.py to show the nearby local business. how can i do that?



i've went through this solution but i'm unable to do that. can anyone help me?



EDIT



i have added ajax to code still not able to send lat value to the views.py



    <!DOCTYPE html>
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<p>Click the button to get your coordinates.</p>

<button id="send-my-url-to-django-button" onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}

}

function showPosition(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
x.innerHTML = "Latitude: " +lat +
"<br>Longitude: " +long ;



$(document).on('click',function() {
var lat1 = lat;
var long1 = long;
$("#send-my-url-to-django-button").click(function() {
$.ajax({
url: '{% url "accounts:location" %}',
//type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: {
'lat': lat1,
'long': long1,
//'csrfmiddlewaretoken': '{{ csrf_token }}'
},
success : function(json) {
alert("Successfully sent the URL to Django");
},
error : function(xhr,errmsg,err) {
alert("Could not send URL to Django. Error: " + xhr.status + ": " + xhr.responseText);
}
});
});
});

}
</script>
</body>
</html


after clicking 3 times, i receive lat ang long values in views.py and in browser it shows




Could not send URL to django Error:200;











share|improve this question
























  • Have you tried HTTP? You know, that thing between your server and your browser.
    – sjahan
    Nov 11 at 8:16










  • HTTP means send data using POST and GET methods?
    – Prince Nihith
    Nov 11 at 8:32













up vote
-1
down vote

favorite
1









up vote
-1
down vote

favorite
1






1





This is the javascript code to get users latitude and longitude.



<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}

}

function showPosition(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
x.innerHTML = "Latitude: " +lat +
"<br>Longitude: " +long ;
}
</script>

</body>
</html>


through that code, I can display the user's latitude and longitude in the front end, but I need to send the lat and long to Django views.py to show the nearby local business. how can i do that?



i've went through this solution but i'm unable to do that. can anyone help me?



EDIT



i have added ajax to code still not able to send lat value to the views.py



    <!DOCTYPE html>
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<p>Click the button to get your coordinates.</p>

<button id="send-my-url-to-django-button" onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}

}

function showPosition(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
x.innerHTML = "Latitude: " +lat +
"<br>Longitude: " +long ;



$(document).on('click',function() {
var lat1 = lat;
var long1 = long;
$("#send-my-url-to-django-button").click(function() {
$.ajax({
url: '{% url "accounts:location" %}',
//type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: {
'lat': lat1,
'long': long1,
//'csrfmiddlewaretoken': '{{ csrf_token }}'
},
success : function(json) {
alert("Successfully sent the URL to Django");
},
error : function(xhr,errmsg,err) {
alert("Could not send URL to Django. Error: " + xhr.status + ": " + xhr.responseText);
}
});
});
});

}
</script>
</body>
</html


after clicking 3 times, i receive lat ang long values in views.py and in browser it shows




Could not send URL to django Error:200;











share|improve this question















This is the javascript code to get users latitude and longitude.



<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}

}

function showPosition(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
x.innerHTML = "Latitude: " +lat +
"<br>Longitude: " +long ;
}
</script>

</body>
</html>


through that code, I can display the user's latitude and longitude in the front end, but I need to send the lat and long to Django views.py to show the nearby local business. how can i do that?



i've went through this solution but i'm unable to do that. can anyone help me?



EDIT



i have added ajax to code still not able to send lat value to the views.py



    <!DOCTYPE html>
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<p>Click the button to get your coordinates.</p>

<button id="send-my-url-to-django-button" onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}

}

function showPosition(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
x.innerHTML = "Latitude: " +lat +
"<br>Longitude: " +long ;



$(document).on('click',function() {
var lat1 = lat;
var long1 = long;
$("#send-my-url-to-django-button").click(function() {
$.ajax({
url: '{% url "accounts:location" %}',
//type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: {
'lat': lat1,
'long': long1,
//'csrfmiddlewaretoken': '{{ csrf_token }}'
},
success : function(json) {
alert("Successfully sent the URL to Django");
},
error : function(xhr,errmsg,err) {
alert("Could not send URL to Django. Error: " + xhr.status + ": " + xhr.responseText);
}
});
});
});

}
</script>
</body>
</html


after clicking 3 times, i receive lat ang long values in views.py and in browser it shows




Could not send URL to django Error:200;








javascript django-views






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 9:46

























asked Nov 11 at 7:47









Prince Nihith

74




74












  • Have you tried HTTP? You know, that thing between your server and your browser.
    – sjahan
    Nov 11 at 8:16










  • HTTP means send data using POST and GET methods?
    – Prince Nihith
    Nov 11 at 8:32


















  • Have you tried HTTP? You know, that thing between your server and your browser.
    – sjahan
    Nov 11 at 8:16










  • HTTP means send data using POST and GET methods?
    – Prince Nihith
    Nov 11 at 8:32
















Have you tried HTTP? You know, that thing between your server and your browser.
– sjahan
Nov 11 at 8:16




Have you tried HTTP? You know, that thing between your server and your browser.
– sjahan
Nov 11 at 8:16












HTTP means send data using POST and GET methods?
– Prince Nihith
Nov 11 at 8:32




HTTP means send data using POST and GET methods?
– Prince Nihith
Nov 11 at 8:32

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246800%2fhow-to-send-user-latitude-and-longitude-to-django-view%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246800%2fhow-to-send-user-latitude-and-longitude-to-django-view%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python