首页 > 解决方案 > django count data from model and show it on template

问题描述

I am trying to count all the data and show it as a number on the template.

here my models.py

class Employee(models.Model):
    uid = models.CharField(max_length=100)
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

here my views.py

class EmployeeListView(ListView):
    context_object_name = 'employees'
    model = models.Employee

my employee.html

 <h5 class='margin-bottom'>Total Data {{ employee_set.count }}</h5>

trying use _set.count but din't work.

标签: djangodjango-templates

解决方案


你需要把{{employees.count}}它通过context_object_name。它是一个查询集,您可以在其上使用count()方法。


推荐阅读