Django开发教程:模板系统中的数值运算

800 阅读3分钟

大家好,我是梓栋!

上一期我们讲了Django中实现全局搜索功能,粉丝renhao想让出一期模板数值运算的教程,它来了。

在之前的推文中我整理了一篇关系模板系统的推文 :如何使用Python中Django模板?

这篇推文详细解释了Django的模板系统,如果还有小伙伴没有掌握的,自己可以去看一下。

今天这边文章主要给大家介绍的是Django中的数值运算,下面我们来详细说明一下。

我们还是以图书管理系统为例,进行讲解。

一、 widthratio函数实现乘除法和百分比显示

1.widthratio函数介绍

模板系统中有 widthratio函数参数如下:

{%  widthratio 参数1 参数2  参数3 %}

参数的使用方法是:参数1/参数2*参数3

2. 实现乘法运算

为了能够使用widthratio函数实现乘法,参数要满足以下要求:

  • 参数1 :乘数
  • 参数2:1
  • 参数3:被乘数

下面我们举例来说明,比如我们现在想计算图书销售额。

img

在views.py中我们只需要查询出图书的数据

def operation(request):
  # 查询所有图书
     book_obj_list = models.Books.objects.all()
     return render(request,"operationa.html",locals())

在模板中我们要实现的公式是

图书销售额 = 图书数量*图书单价

我们在前端中这样写

<td>{% widthratio book_obj.book_sales 1 book_obj.book_price %}</td>

widthratio函数的参数配置‍如下:

  • 参数1:book_obj.book_sales 图书销量
  • 参数2:1
  • 参数3:book_obj.book_price 图书价格

前端效果如下图 img 全部html代码如下

••<div class="content-wrapper">
<div class="container-fluid">
•
            <div class="card-header border-0 bg-transparent bg-dark text-white">
                高销量的图书
            </div>
            <div class="table-responsive">
•
                <table class="table table-dark table-striped shadow-dark"
                       role="grid" aria-describedby="default-datatable_info">
                    <thead>
                    <tr role="row">
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Position: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书</p>
                        </th>
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Office: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书销量</p>
                        </th>
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Office: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书销量额</p>
                        </th>
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Office: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书价格</p>
                        </th>
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Office: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书评分</p>
                        </th>
•
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Office: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书库存</p>
                        </th>
•
                    </tr>
                    </thead>
                    <tbody>
                    {% for book_obj in book_obj_list%}
                        <tr role="row" class="odd">
•
•
                            <!-- 使用弹框来显示图书的详情-->
                            <td>
                                <img src="{% static book_obj.image_set.first.img_address.name %}" class="product-img"
                                     alt="product img">
•
                            </td>
                            <td>{{ book_obj.book_sales }}</td>
                            <td>{% widthratio book_obj.book_sales 1 book_obj.book_price %}</td>
•
•
                            <td>{{ book_obj.book_price }}</td>
                            <td>{{ book_obj.book_score }}</td>
                            <td>
                                <div class="progress shadow" style="height: 4px;">
                                    <div class="progress-bar gradient-scooter" role="progressbar"
                                            {#用除法来计算百分比#}
                                         style="width: {% widthratio book_obj.book_inventory 1000 100 %}%"></div>
                                </div>
                            </td>
•
                        </tr>
                    {% endfor %}
•
•
                    </tbody>
•
                </table>
            </div>
        </div>
    </div>

2. 实现除法运算

为了能够使用widthratio函数实现除法,参数要满足以下要求:

  • 参数1 :除数
  • 参数2 :被除数
  • 参数3 :当参数为1时,值为正常除法,当参数为100时,是为了计算百分比

下面举例来说明,假如要计算每本图书销量占同类书籍总量占比,为了方便计算我们规定每类图书总量是1000。

模板中我们要实现的公式如下

图书销量占比 = 每本数的销量/每类图书总量

在前端中是这样写的

 <td>{% widthratio  book_obj.book_sales 1000 100%}%</td>

widthratio函数的参数配置如下:

  • 参数1:book_obj.book_sales 图书销量
  • 参数2:1000 每类图书总量
  • 参数3:100 求出占比后乘以100加%就是百分比

前端效果如下图 img前端全部代码如下

 <div class="content-wrapper">
        <div class="container-fluid">
•
            <div class="card-header border-0 bg-transparent bg-dark text-white">
                高销量的图书
            </div>
            <div class="table-responsive">
•
                <table class="table table-dark table-striped shadow-dark"
                       role="grid" aria-describedby="default-datatable_info">
                    <thead>
                    <tr role="row">
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Position: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书</p>
                        </th>
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Office: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书销量</p>
                        </th>
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Office: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书销量额</p>
                        </th>
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Office: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书销量占比</p>
                        </th>
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Office: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书价格</p>
                        </th>
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Office: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书评分</p>
                        </th>
•
                        <th class="sorting" tabindex="0" aria-controls="default-datatable"
                            rowspan="1" colspan="1"
                            aria-label="Office: activate to sort column ascending"
                            style="width: 100px;font-size:17px"><p>图书库存</p>
                        </th>
•
                    </tr>
                    </thead>
                    <tbody>
                    {% for book_obj in book_obj_list%}
                        <tr role="row" class="odd">
•
                            <!-- 使用弹框来显示图书的详情-->
                            <td>
                                <img src="{% static book_obj.image_set.first.img_address.name %}" class="product-img"
                                     alt="product img">
•
                            </td>
                            <td>{{ book_obj.book_sales }}</td>
                            <td>{% widthratio book_obj.book_sales 1 book_obj.book_price %}</td>
                            <td>{% widthratio book_obj.book_sales 1000 100 %}%</td>
                            <td>{{ book_obj.book_price }}</td>
                            <td>{{ book_obj.book_score }}</td>
                            <td>
                                <div class="progress shadow" style="height: 4px;">
                                    <div class="progress-bar gradient-scooter" role="progressbar"
                                            {#用除法来计算百分比#}
                                         style="width: {% widthratio book_obj.book_inventory 1000 100 %}%"></div>
                                </div>
                            </td>
•
                        </tr>
                    {% endfor %}
•
•
                    </tbody>
•
                </table>
            </div>
        </div>
    </div>

3.小数点后面保留几位

由于上面的widthratio方法无法实现准确保留小数位,我们要采用过滤器floatformat的方式来实现计算每本书销量占比后面保留2位

在views.py中我们要计算出每本书的销量占比,代码如下:

def operation(request):
    # 查询所有图书
    book_obj_list = models.Books.objects.all()
    res_lst = []
    for book_obj in book_obj_list:
        res_dic = {
            "book_obj":book_obj,# 图书对象
            "book_rate":book_obj.book_sales/1000 # 销量占比
        }
        print(res_dic)
        res_lst.append(res_dic)
•
    return render(request,"operationa.html",locals())

html中的代码中显示保留2位小数的值如下:

<td>{{ res_dic.book_rate | floatformat:2}}</td>

前端实现效果如图:

img

说明:要实现保留小数后面3位,只需要这样写

<td>{{ res_dic.book_rate | floatformat:3}}</td>

前端全部代码如下:

 {% for res_dic in res_lst %}
        <tr role="row" class="odd">
            <!-- 使用弹框来显示图书的详情-->
            <td>
                <img src="{% static res_dic.book_obj.image_set.first.img_address.name %}" class="product-img"
                     alt="product img">
•
            </td>
            <td>{{ res_dic.book_obj.book_sales }}</td>
            <td>{% widthratio res_dic.book_obj.book_sales 1 res_dic.book_obj.book_price %}</td>
              <td>{{ res_dic.book_rate | floatformat:2}}</td>
            <td>{{ res_dic.book_obj.book_price }}</td>
            <td>{{ res_dic.book_obj.book_score }}</td>
•
            <td>
                <div class="progress shadow" style="height: 4px;">
                    <div class="progress-bar gradient-scooter" role="progressbar"
                            {#用除法来计算百分比#}
                         style="width: {% widthratio res_dic.book_obj.book_inventory 1000 100 %}%"></div>
                </div>
            </td>
•
        </tr>
    {% endfor %}

二、 过滤器add实现加减法

其实这个操作就简单了,视图函数我们还是和上面的一样没有改变。

1.实现加法运算

举例说明,如果想让每本书的销量增加100

<td>{{ res_dic.book_obj.book_sales|add:100 }}</td>

2.实现减法运算

举例说明,如果项目每本数据库存减少10

 <td>{{ res_dic.book_obj.book_inventory| add:-10 }}</td>

3.混合运算

举例说明,如果想计算出每本图书销量增加100之后的销售额

<td>{% widthratio res_dic.book_obj.book_sales|add:100  1  res_dic.book_obj.book_price  %}</td>

模板系统的数值运算介绍到这里。

我是梓栋,我们下期见!