简体   繁体   中英

GeoDjango OSMWidget coordinate change

I want to know if there's a way to know when the OSMWidget coordinates have been changed, I'm pretending to reflect this change on the Longitude and Latitude fields. I have the following form:

from django.contrib.gis import forms
from .models import Branch


class BranchCreateForm(forms.Form):
    name = forms.CharField(label ="Name", max_length=120)
    image_facade = forms.ImageField( label="Image Facade")
    longitude = forms.DecimalField(label = "Latitude", max_digits=9, decimal_places=6)
    latitude = forms.DecimalField(label = "Longitude", max_digits=9, decimal_places=6)
    location = forms.PointField(
        widget=forms.OSMWidget(
            attrs={'map_width': 600,
                   'map_height': 400,
                   'template_name': 'gis/openlayers-osm.html',
                   'default_lat': 42.1710962,
                   'default_lon': 18.8062112,
                   'default_zoom': 6}))

A PointField contains the longitude and latitude coordinates of the Point object that it creates . Therefore you don't need to save them separately as DecimalField s.

Whenever a change is made through the widget and the form is saved, the Point is updated accordingly.

Let's assume that you have a Branch instance then you can access the coordinates as follows:

br = Branch.objects.get(pk=1)
longitude = br.location.x
latitude = br.location.y

EDIT After declaration Provided by OP:

You don't need to add the fields in the form class.
What you need is to access the specific form fields in the template:

{{ form.location.x|default_if_none:"" }}
{{ form.location.y|default_if_none:"" }}

Source: Display value of a django form field in a template?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM