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>

3
  • 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. Commented Jan 31, 2019 at 11:05
  • Please check screen shot, i attached now, @dirkgroten Commented Jan 31, 2019 at 11:17
  • Again, you know how to add the user count to the context, do the same for the employee count. Commented Jan 31, 2019 at 11:19

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

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
0

This can also be done if you have object in template for example You have object passed to template

users = User.objects.all().order_by('-id')

Then you can use

<p>{{users.count}}</p>

This will give object count even if you pass object directly

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.