GeoDjango OSMWidget坐标更改

时间:2019-03-20 17:57:27

标签: python django django-forms geodjango

我想知道是否有办法知道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}))

1 个答案:

答案 0 :(得分:0)

PointField包含Point object that it createslongitudelatitude坐标。因此,您无需将它们另存为DecimalField

只要通过小部件进行更改并保存表单,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:"" }}

来源:Display value of a django form field in a template?