I'm new to Django, and I'm stuck on a template tag that I can't figure out how to get working. I know I'm missing something in my view but I have written it in several different ways and can't seem to find the right way to do this. I have a Morris chart in my app that I am trying to provide information to. I want to show the percentage of operators that are available. In my model, I have a Boolean value that says if the operator is_available. When I pass it back to the template I want the template tag to run the percentage and pass back the value to the morris pie chart.
Here is my view:
@login_required(login_url='login/')
def operator(request):
operators = Operator.objects.all()
operator_status = Operator.objects.values_list('is_available', flat=True)
context = {
'operators': operators,
'operators_available': operator_status,
}
return render(request, 'content/operator.html', context)
This is the template tag in use:
<div class="widget-detail-1">
<h2 class="p-t-10 m-b-0"> {{ operators_available | percentage_of:True }} </h2>
</div>
</div>
and finally my template tag:
@register.filter(name='percentage_of')
def percentage_of(part, whole):
try:
return "%d"[2:] % (float(part) / whole * 100)
except (ValueError, ZeroDivisionError):
return "Division by Zero"
Its still a bit confusing what you actually want to achieve and how your
Operators
model actually look like and what values your variables contain. But I will try to make some guesses of what you want to do and try to give you an answer.It seems as if you mix together the use of
operators
andoperators_available
and also you mix the usage of data types such as floats and booleans.Lets go through your code...
In your Template you write the following
This equals to a function call of
percentage_of(operators_available, True)
. Remember also thatoperators_available
comes from your.valus_list('is_available')
and is a boolean. So what you're actually doing is something likepercentage_of(True, True)
.Inside
percentage_of
you then try to apply math to these boolean values with(float(part) / whole * 100)
, or actually more likefloat(True) / True * 100
.The Solution
Make sure that the values that you pass to the context is in the format you expect it to. It currently look like you think that you're passing float values, but actually are passing boolean values. Try to debug or print the values before applying your template tag to them.