How to count the number of elements of each category in Django?
Question:
I have a list of elements. There is a ‘status’ value here. I need a code that in an HTML page will show the total number of items for each ‘status’. How to do it?
For intance, there is the list in the page:
appnumber status
№54534534 accepted
%46342322 in process
%55745232 accepted
%67456452 denied
%76454534 accepted
%52864525 denied
%86752525 accepted
The result should be:
accepted - 4
denied - 2
in process - 1
My code:
home.html
<div class="headtext">
<form method="GET" action="{% url 'search' %}">
<input type="search" type="text" name="q" prequired 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 %}
</div>
views.py
class HomeView(ListView):
model = Application
template_name = 'home.html'
class Search(ListView):
template_name = 'home.html'
def get_queryset(self):
return Application.objects.filter(appnumber__icontains=self.request.GET.get("q"))
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context["q"] = self.request.GET.get("q")
return context
Answers:
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>
I have a list of elements. There is a ‘status’ value here. I need a code that in an HTML page will show the total number of items for each ‘status’. How to do it?
For intance, there is the list in the page:
appnumber status
№54534534 accepted
%46342322 in process
%55745232 accepted
%67456452 denied
%76454534 accepted
%52864525 denied
%86752525 accepted
The result should be:
accepted - 4
denied - 2
in process - 1
My code:
home.html
<div class="headtext">
<form method="GET" action="{% url 'search' %}">
<input type="search" type="text" name="q" prequired 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 %}
</div>
views.py
class HomeView(ListView):
model = Application
template_name = 'home.html'
class Search(ListView):
template_name = 'home.html'
def get_queryset(self):
return Application.objects.filter(appnumber__icontains=self.request.GET.get("q"))
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context["q"] = self.request.GET.get("q")
return context
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>