2017-02-23 52 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

完美的作品!謝謝 –