I am currently working on developing a convenient nearby shops application that utilizes django and GeoDjango to display the shops closest to the user's location. Instead of hard-coding the nearest shop, I aim to retrieve the user's location from their browser for more accuracy.
views.py
from django.views import generic
from django.contrib.gis.geos import Point
from django.contrib.gis.db.models.functions import Distance
from .models import Shop
longitude = -80.191_788
latitude = 25.761_681
user_location = Point(longitude, latitude, srid=4326)
class Home(generic.ListView):
model = Shop
context_object_name = "shops"
queryset = Shop.objects.annotate(
distance=Distance("location", user_location)
).order_by("distance")[0:6]
template_name = "Home.html"
models.py
class Shop(models.Model):
name = models.CharField(max_length=100)
location = models.PointField()
address = models.CharField(max_length=100)
city = models.CharField(max_length=50)