Reputation: 1270
I am building a BlogApp and I am stuck on a Problem.
What i am trying to do
I am trying to access user location like :- City or State. I don't want to access in Map. I just want to access the city name of users.
What have i tried
I try through JavaScript
and Google Apis
to access location , It showed the location perfectly. BUT i didn't find any way to save the location of User in Database from FrontEnd to BackEnd. So i dropped down that method. The Below ↓ code is showing the FrontEnd Code of JavaScript
.
<script>
let thisElement = $(this)
$.ajax({
url: "https://geolocation-db.com/jsonp","/update_counter/",
jsonpCallback: "callback",
dataType: "jsonp",
success: function(location) {
$('#country').html(location.country_name);
console.log('success')
thisElement.parent().parent().fadeOut();
}
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div>Country: <span id="country"></span></div>
Then i tried MaxMind's GeoIP
it also didn't work for me. ( Also answers are too Old ).
Then i tried THIS BUT they are not connected to any model, so it may be difficult to save user location in DataBase.
I don't know what to do.
Any help would be appreciated.
Thank You in Advance.
Upvotes: 0
Views: 266
Reputation: 397
$( "<your element with City name>" ).change(function() { const city = <something where you wrote your City>; $.ajax ({ url : <your url>, data: { 'city': city, }, success: function (data) { <do something if you want>; } }); });
url:
...
path('<your url>', views.your_function, name='ajax-something'),
...
views:
def your_function(request):
city = request.GET.get('city')
<here you write data to db>
return <what you want to do>
Upvotes: 1
Reputation: 660
Use Ajax.
After getting your location. Use Ajax to send it to django and them use django to save to db.
Upvotes: 0