I also want to pick the location through Django REST API Through template..please suggest necessary django packages for it and how to write locationField in the Django Model
This is the picture that django location field exactly which I want to Add please see it

You can create the multiple inputs field subclassing MultiValueField:
class Location:
def __init__(self, latitude, longitude):
self.latitude = latitude
self.longitude = longitude
class LocationField(MultiValueField):
def __init__(self, *args, **kwargs):
fields = (
FloatField(max_value=90, min_value=-90),
FloatField((max_value=90, min_value=-90)
)
super().__init__(*args, **kwargs)
def compress(self, data_list):
latitude, longitude = data_list
return Location(latitude, longitude)
As for the second question, sorry, but it's very unclear what you are asking for.
I will try to be precise, I have build one similar project. As you described in image in the link. The following would do the job. Make these changes in settings.py
'django.contrib.gis.db.backends.postgis',
Install this package https://pypi.org/project/django-map-widgets/ Add the below lines in Installed Apps:
'django.contrib.gis',
'mapwidgets'
Add these variables:
GOOGLE_MAP_API_KEY = "Create a google_map_api_key_and_past_it_here"
MAP_WIDGETS = {
"GOOGLE_MAP_API_KEY": "Create a google_map_api_key_and_past_it_here"
}
In the models.py:
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point
class Stack(models.Model):
location = models.PointField(geography=True, spatial_index=True)
In forms.py:
from django.contrib.gis import forms as gis_forms
from django.contrib.gis.db import models as gis_models
from mapwidgets.widgets import GooglePointFieldWidget
class GoogleAddressForm(gis_forms.ModelForm):
location = gis_forms.PointField(
widget=widgets.GooglePointFieldWidget(
),
)
class Meta:
model = Point
fields = "__all__"
formfield_overrides = {
gis_models.PointField: {"widget": widgets.GooglePointFieldWidget()}
}
In view simply render the template and pass the form in context.
In template using {{form.location}} would render the exact image with all those functionalities.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With