[Answered ]-How to calculate percentage with facility in Django 1.7

2👍

You just need to override method get_context_data. And your first trial was almost there.

class MyView(TemplateView):
    template_name = 'template.html'

    def get_context_data(self, **kwargs):
        genders = Person.objects.values('gender').annotate(cnt=Count('gender')).order_by('gender')
        total_items = Person.objects.count()
        items = [
            {'gender': g['gender'], 'value': g['cnt'] * 100 / total_items} for g in genders
        ]
        return {'items': items}
👤ZZY

Leave a comment