django - 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.
解决方案
你需要把{{employees.count}}
它通过context_object_name
。它是一个查询集,您可以在其上使用count()
方法。
推荐阅读
- python - 更正 ELIF 语句产生语法错误 python 3.6.2
- .net - Selenium ChromeDriver 的方法在使用 ExecuteAsyncScript 后被阻塞(等待来自 JS 的结果)?
- c# - C# 返回一个完全不相关且看似随机的整数?
- javascript - 如何使用刷新令牌更新 Outlook 2.0 API 令牌?
- getgauge - Taiko 抛出错误:无法启动 chrome 没有可用的沙箱
- python - Pyqt5打开另一个文件*.py Pb的类的子窗口
- wso2 - 使用分页从 Web API 接收所有结果
- python - datetime.datetime.strptime 将字符串转换为 datetime 它发生错误未转换日期要求:2
- python - 以 django 形式预填充图像字段不起作用
- json - 第二次加载时Volley显示旧数据