1👍
✅
You can use Count
with annotate()
so:
from django.db.models import Count
class HomeView(ListView):
model = Application
template_name = 'home.html'
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
status_counts = Application.objects.values('status').annotate(total=Count('status'))
context['status_counts'] = status_counts
return context
Then you can use status_counts
in Template so:
<div class="headtext">
<form method="GET" action="{% url 'search' %}">
<input type="search" type="text" name="q" required placeholder="Put appnumber">
<button type="submit">Find</button>
</form>
</div>
<div>
{% for application in object_list %}
<div>
<p>Application: {{ application.appnumber }}, status: {{ application.status }}</p>
</div>
{% endfor %}
<h2>Status Counts</h2>
<ul>
{% for status_count in status_counts %}
<li>{{ status_count.status }} - {{ status_count.total }}</li>
{% endfor %}
</ul>
</div>
Source:stackexchange.com