【问题标题】:GeoDjango OSMWidget coordinate changeGeoDjango OSMWidget 坐标变化
【发布时间】:2019-08-11 12:51:12
【问题描述】:

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

【问题讨论】:

    标签: python django django-forms geodjango


    【解决方案1】:

    PointField 包含Point object that it createslongitudelatitude 坐标。因此您无需将它们单独保存为DecimalFields。

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

    【讨论】:

    • 该模型没有经度和纬度字段,我只是在模板中使用它们,仅用于可视化目的,例如,如果用户正在创建新分支,并使用地图更改默认点,我希望这反映在纬度和经度输入字段中。
    • @katbarahona 我已经更新了我的答案,这符合您的需求吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 2011-09-29
    • 2011-10-15
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多