我想知道是否有办法知道OSMWidget坐标何时被更改,我假装在经度和纬度字段上反映这一变化。我有以下表格:
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}))
PointField
包含longitude
的latitude
和Point
object that it creates坐标。因此,您不需要将它们单独保存为DecimalField
s。
每当通过窗口小部件进行更改并保存表单时,都会相应地更新Point
。
假设您有一个Branch
实例,那么您可以访问坐标,如下所示:
br = Branch.objects.get(pk=1)
longitude = br.location.x
latitude = br.location.y
编辑后声明由OP提供:
您不需要在表单类中添加字段。 您需要的是访问模板中的特定表单域:
{{ form.location.x|default_if_none:"" }}
{{ form.location.y|default_if_none:"" }}