0

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>

Parth Jani
  • 33
  • 1
  • 4
  • 12
  • 2
    just do the same as for counting the User objects, I don't understand what the problem is. If `User.objects.count()` counts the users, `Employee.objects.count()` counts the employees. – dirkgroten Jan 31 '19 at 11:05
  • Possible duplicate of [Getting a count of objects in a queryset in django](https://stackoverflow.com/questions/5439901/getting-a-count-of-objects-in-a-queryset-in-django) – Dragon Ball Jan 31 '19 at 11:17
  • Please check screen shot, i attached now, @dirkgroten – Parth Jani Jan 31 '19 at 11:17
  • Again, you know how to add the user count to the context, do the same for the employee count. – dirkgroten Jan 31 '19 at 11:19

1 Answers1

4

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>
Sergey Pugach
  • 5,561
  • 1
  • 16
  • 31
  • Thank you so much its working now fantastically, I know its very easy, but i am new to Django so i am little bit confused with code.again thanks – Parth Jani Jan 31 '19 at 11:33