2017-02-23 50 views
0

在Django的1.9/Python 2.7版,我有以下几点看法:如何圆一个GeoDjango内置距离

qs = ThePlace.objects.filter(lnglat__distance_lte=(lnglat, D(km=30))).distance(lnglat).order_by('distance') 

在我的模板,我显示的距离,当我遍历的对象:

{% for qsplace in qs %} 
    {{ qsplace.distance }} 
{% endfor %} 
例如显示“25.717617095米”的

我想显示圆角(26米)。如果1公里多,我想显示例如3公里

我公司开发的第一templatetag圆的数字:

from django import template 
register = template.Library() 

@register.filter 
def dividebyth(value): 
    print(type(value)) 
    value = round(value, 2) 
    return value 

,放在我的模板: {{qsplace.distance | dividebyth}}

这将导致以下错误:

TypeError: a float is required 

看来我无法圆一个远程对象。

任何方式来管理?

谢谢!

回答

1

docs

Because the distance attribute is a Distance object, you can easily express the value in the units of your choice. For example, city.distance.mi is the distance value in miles and city.distance.km is the distance value in kilometers.

要获得距离值浮法米试试这个:{{ qsplace.distance.m|dividebyth }}

+1

完美的作品!谢谢 –