Hello,
I have a small problem in my simple application.
Background
I have two views (home and search). On the home view, there is a search bar where the user types in a name of a service and the ‘search’ view is rendered. The search view (which requires a service name) then filters through my Shop model and finds all shops offering the service. In my home view template, I have the following JS script:
const findLoc = () => {
var URL = "{% url 'home' %}"
var django_csrf_token = '{{ csrf_token }}'
const success = (position) => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
// console.log("longitude: " + longitude)
// console.log("latitude: " + latitude)
var data = {'latitude': latitude,
'longitude': longitude,
'csrfmiddlewaretoken': django_csrf_token };
$.post(URL, data);
}
const error = () => {
console.log('Location gathering Not allowed')
const latitude = 43.6771296 // Default Latitude
const longitude = -79.6333512 // Default Longtitude
var data = {'latitude': latitude,
'longitude': longitude,
'csrfmiddlewaretoken': django_csrf_token };
$.post(URL, data);
}
navigator.geolocation.getCurrentPosition(success,error);
}
// Location is fetched when user loads screen
Window.onload = findLoc();
// Location is fetched when user presses enter on home screen
input_field = document.getElementById('service_name')
input_field.addEventListener('keydown', (e) => {
if (e.key =='Enter'){
findLoc();
}
});
// Location is fetched when user clicks on search button
document.getElementById('searchButton').addEventListener('click', findLoc)
This script is basically sending user coordinates to the home view. In my search view, I have the coordinates of each shop and ultimately I want to display the distance from the user for each shop while rendering the search view, for this I will be using a Google API.
Problem
My problem is that the user longitude and latitude are extracted on the ‘home’ view and I don’t want to pass this information to the ‘search’ view via URL, because I don’t want to show this information in my URL path.
The other option I tried was to add the above JS on the ‘search’ view but I am not able to extract and post the user longitude and latitude like I did on the ‘home’ view.
def search(request, service_name):
if request.method == 'POST':
user_lng = request.POST.get('longitude')
user_lat = request.POST.get('latitude')
print(user_lng, user_lat)
else:
print('nothing')
shops = Shop.objects.filter(services__name__icontains = service_name).order_by("-created_on").distinct()
return render(request, 'main/search.html', context = {
'searched':service_name,
'shops_found': shops})
Is there a better way of doing this? I know how to calculate the distance but I need to have the coordinates of the from location and to location in my search view so I can execute my downstream logic. (For each shop, I will save the distance which is a field of my shop model. Then I can re-filter the shops query set and order by distance and then send that to my search template.)