How to pass lat and lng to Django view without button click
pos = {lat: position.coords.latitude, lng: position.coords.longitude,};
I am working on a Django project and trying to get the current location of the user. i have a javascript function that returns the latitude and longitude , but i cannot pass them to Django as variables to save them or to do anything with them. I am using google maps api.
**You can use Django view parameter to pass some data to your views : **
views.py
def coordinates(request, lat, long):
// You may try to convert the lat and long variables
// in the good type, i suppose here they are float
my_lat, my_long = Float(lat), Float(long)
context = {'lat': my_lat, 'long': my_long}
return render(request, 'coordinates.html', context)
urls.py
from app_name import views
path('coordinates/<str:lat>/<str:long>/', views.coordinates, name='coordinates')
NB : I passe the lat and long here as string, but in the view i convert them with Float().
And this is an example of url: http://localhost:8000/coordinates/3.45/4.57
.html
<section>
The latitude is : {{ lat }}
<br>
The longitude is : {{ long }}
</section>