gpt4 book ai didi

django - 点场的纬度/经度小部件?

转载 作者:行者123 更新时间:2023-12-01 19:24:05 25 4
gpt4 key购买 nike

是否有用于 PointField 的小部件作为单独的纬度/经度输入?就像 DateTimeField 的 SplitDateTimeWidget 一样。

最佳答案

这是我的工作自定义字段和小部件:

class LatLongWidget(forms.MultiWidget):
"""
A Widget that splits Point input into two latitude/longitude boxes.
"""

def __init__(self, attrs=None, date_format=None, time_format=None):
widgets = (forms.TextInput(attrs=attrs),
forms.TextInput(attrs=attrs))
super(LatLongWidget, self).__init__(widgets, attrs)

def decompress(self, value):
if value:
return tuple(reversed(value.coords))
return (None, None)

class LatLongField(forms.MultiValueField):

widget = LatLongWidget
srid = 4326

default_error_messages = {
'invalid_latitude' : _('Enter a valid latitude.'),
'invalid_longitude' : _('Enter a valid longitude.'),
}

def __init__(self, *args, **kwargs):
fields = (forms.FloatField(min_value=-90, max_value=90),
forms.FloatField(min_value=-180, max_value=180))
super(LatLongField, self).__init__(fields, *args, **kwargs)

def compress(self, data_list):
if data_list:
# Raise a validation error if latitude or longitude is empty
# (possible if LatLongField has required=False).
if data_list[0] in validators.EMPTY_VALUES:
raise forms.ValidationError(self.error_messages['invalid_latitude'])
if data_list[1] in validators.EMPTY_VALUES:
raise forms.ValidationError(self.error_messages['invalid_longitude'])
# SRID=4326;POINT(1.12345789 1.123456789)
srid_str = 'SRID=%d'%self.srid
point_str = 'POINT(%f %f)'%tuple(reversed(data_list))
return ';'.join([srid_str, point_str])
return None

关于django - 点场的纬度/经度小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17021852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com