【问题标题】:How to count total objects of a database table in django?如何计算 django 中数据库表的对象总数?
【发布时间】:2019-06-24 19:51:04
【问题描述】:

我的问题是关于 django 应用程序,我在班级员工的一个模型中有一个应用程序,我想在模板的前端显示有多少员工注册。

在下面的应用程序用户计数已成功计算。

我想从模板表单和模板显示中输出 4 名员工注册 4 名员工在前台注册

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
    })

模型.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 模板

{% 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>

【问题讨论】:

  • 只是对用户对象进行计数,我不明白问题出在哪里。如果User.objects.count() 计算用户,Employee.objects.count() 计算员工。
  • 请检查屏幕截图,我现在附上,@dirkgroten
  • 再次,您知道如何将用户计数添加到上下文中,对员工计数执行相同操作。

标签: django python-3.x django-models django-views django-template-filters


【解决方案1】:

就像你为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,
    })

并将其放入您的用户模板中:

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

对于员工:

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

【讨论】:

  • 非常感谢它现在工作得非常好,我知道它很容易,但我是 Django 新手,所以我对代码有点困惑。再次感谢
猜你喜欢
  • 1970-01-01
  • 2013-03-16
  • 2014-02-26
  • 2021-06-09
  • 1970-01-01
  • 2014-05-24
  • 2018-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多