0
How to divide two values inside django template?
I have two object attributes and I want to divide them and show in page <p>{{question.votes}} | div : {{choice.votes}}</p>
2 Antworten
0
In view make a new object list that contains questions and answers and in template use a for loop.
0
In template do this
{{ scores|div:weights|mul:100 }}
I did this to get percentage of actual score. You did not provide your view code but suggest the above code for the sake of any one still having same problem since question is old.
In my view I have something like
class CreditriskDetailView(ListView):
title = "Credit Risk Details"
def get_title(self):
return self.title
model = Rating
template_name = "creditrisk/rating/detail.html"
def get_context_data(self, *args, **kwargs):
context = super(CreditriskDetailView, self).get_context_data(*args, **kwargs)
context['rating_list'] = Rating.objects.filter(slug=self.kwargs ['slug'])
context['rating_score'] = Rating.objects.filter(slug=self.kwargs ['slug']).aggregate(Sum('score'))
context['weights'] = Rating.objects.filter(slug=self.kwargs ['slug']).aggregate(total_weight=Sum('weight'))['total_weight']
context['scores'] = Rating.objects.filter(slug=self.kwargs ['slug']).aggregate(total_score=Sum('score'))['total_score']
context["title"] = self.get_title()
return context
Don't forget to import sum in your as below
from django.db.models import Sum