【发布时间】:2018-06-20 10:52:56
【问题描述】:
我需要你的帮助来找出我的代码中的错误。
我正在尝试在用户输入地址后自动填充纬度/经度字段。因此,位置字段地图将显示,并且两个字段都将自动填充。
models.py:
from location_field.models.plain import PlainLocationField
class Store(models.Model):
building = models.ForeignKey(Building, related_name='building', on_delete=models.SET_NULL, blank=True, null=True)
address = models.TextField(default='Singapore')
latitude = models.FloatField(validators=[MinValueValidator(-90.0), MaxValueValidator(90.0)])
longitude = models.FloatField(validators=[MinValueValidator(-180.0), MaxValueValidator(180.0)])
location = PlainLocationField(based_fields=['address'], zoom=7, null=True)
@property
def latitude(self):
if not self.location:
return
try:
print('Store Location1: ' + str(self.location))
latitude, _ = self.location.split(',')
except Exception as e:
print('Exception1: ' + str(e))
print("latitude:", latitude)
return latitude
@property
def longitude(self):
if not self.location:
return
try:
print('Store Location2: ' + str(self.location))
_, longitude = self.location.split(',')
except Exception as e:
print('Exception2: ' + str(e))
print("longitude:", longitude)
return longitude
当启动我的服务器时,我看到以下两个打印语句-
Store Location1: 8 SHENTON WAY #43-01 AXA TOWER Singapore
Exception1: not enough values to unpack (expected 2, got 1)
从不打印经度函数中的打印语句。当我去我的商店时,它在浏览器中显示以下错误-
File "E:\mysite\models.py", line 216, in latitude
latitude, _ = self.location.split(',')
Exception Type: ValueError
Exception Value:not enough values to unpack (expected 2, got 1)
谁能帮我找出确切的问题?
【问题讨论】:
-
在您打印的字符串中,没有逗号。所以用逗号分割字符串会得到一个单一元素的列表,不能解包成两个变量。
-
谢谢,我明白了,这是我的错误。那么您能告诉我如何更改我的功能代码以使其正常工作吗?
-
完全不相关,但您的
latitude和longitude属性会影响同名字段。 -
@brunodesthuilliers 我同意,我将从代码和数据库中删除这两个属性。
标签: python python-3.x django-2.0