Search code examples
djangopython-3.xdjango-modelsdjango-viewsdjango-template-filters

How to count total objects of a database table in django?


My question is about django app, i have one app in one model in class employee ,i want to display how many employee register in fronted side in template.

In below app user count is successfully worked.

I want output like 4 employee register from template form and in template display 4 Employee registered at frontside

Front end side - image

views.py

from django.shortcuts import render,redirect
from django.contrib.auth.forms import UserCreationForm,AuthenticationForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django_adminlte.forms import EmployeeForm
from django_adminlte.models import Employee

def emp(request):
    if request.method == "POST":
        form = EmployeeForm (request.POST) # here "form" is one varible
        if form.is_valid():
            try:
                form.save()
                return redirect("/show")
            except:
                pass
    else:
        form = EmployeeForm()
    return render(request,"employee/employee_index.html",{'form':form})

#i want like this below code(this code is working for count user in front side)

def home(request):
    count = User.objects.count()
    return render(request,'index.html',{
        'count' : count
    })

model.py

from django.db import models

class Employee(models.Model):
    eid = models.CharField(max_length=20)
    ename = models.CharField(max_length=20)
    eemail = models.EmailField()
    econtact = models.CharField(max_length=15)

    class Meta:
        db_table = "employee"

    def __str__(self):
        return self.ename

HTML Template

{% extends 'adminlte/base.html' %}

{% block content %}

      <div class="col-lg-3 col-xs-6">
      <!-- small box -->
<div class="small-box bg-yellow">
    <div class="inner">
        <h3>{{ count }}</h3>
        <p>User Registrations</p>
    </div>
    <div class="icon">
          <i class="fas fa-user-plus"></i>
    </div>
    <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
</div>

  <div class="col-lg-3 col-xs-6">
      <!-- small box -->
   <div class="small-box bg-yellow">
    <div class="inner">
        <h3></h3>
        <p>Total Employee</p>
    </div>
    <div class="icon">
          <i class="fas fa-user-plus"></i>
    </div>
    <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
</div>


Solution

  • Just do the same as you did for User

    def home(request):
        user_count = User.objects.count()
        employee_count = Employee.objects.count()
        return render(request,'index.html',{
            'user_count' : user_count,
            'employee_count' : employee_count,
        })
    

    And put it in your template for user:

    <div class="inner">
        <h3>{{ user_count }}</h3>
        <p>User Registrations</p>
    </div>
    

    and for Employee:

    <div class="inner">
        <h3>{{ employee_count }}</h3>
        <p>Total Employee</p>
    </div>