In Django 1.9 / Python 2.7, I have the following view:
qs = ThePlace.objects.filter(lnglat__distance_lte=(lnglat, D(km=30))).distance(lnglat).order_by('distance')
In my template, I display the distances when I loop over the objects:
{% for qsplace in qs %}
{{ qsplace.distance }}
{% endfor %}
which displays "25.717617095 m" for example.
I would like to display rounded figures (26 m). If more than 1 km, I would like to display 3 km for example
I have developed a first templatetag to round the figures:
from django import template
register = template.Library()
@register.filter
def dividebyth(value):
print(type(value))
value = round(value, 2)
return value
And put in my template: {{ qsplace.distance|dividebyth }}
which results in the following error:
TypeError: a float is required
It seems I cannot round a Distance object.
Any way to manage that ?
Thank you!
From docs :
Because the
distance
attribute is aDistance
object, you can easily express the value in the units of your choice. For example,city.distance.mi
is the distance value in miles andcity.distance.km
is the distance value in kilometers.
To get distance value as float in meters try this: {{ qsplace.distance.m|dividebyth }}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.