django的模板里一个tags可以实现加减乘除法
django的模板里面是用一个tags可以实现,加法,减法,乘法,除法的运算。嘿嘿
先看一下django 官方的解释
For creating bar charts and such, this tag calculates the ratio of a given
value to a maximum value, and then applies that ratio to a constant.
For example::
<img src='bar.gif' height='10' width='{% widthratio this_value max_value 100 %}' />
Above, if ``this_value`` is 175 and ``max_value`` is 200, the image in
the above example will be 88 pixels wide (because 175/200 = .875;
.875 * 100 = 87.5 which is rounded up to 88).
这说明,widthratio 通常用来显示图表,比例时用的,一个数字占了多少百分比等。但仔细想想,如果将某些数字变成特定的数字,不就是乘除法运算了吗?请看:
乘法 A*B: {% widthratio A 1 B %}
除法 A/B: {% widthratio A B 1 %}
利用 add 这个filter ,可以做更疯狂的事:
计算 A^2: {% widthratio A 1 A %}
计算 (A+B)^2: {% widthratio A|add:B 1 A|add:B %}
计算 (A+B) * (C+D): {% widthratio A|add:B 1 C|add:D %}
当然,这种方法在django中实现乘除法比较变态,看起来也不爽,更好的方法,是可以扩展自己的标签。在后面打算自己扩展一个计算乘法的标签,应该好很多。
在django中实现其它一些简单的计算,参考这篇文章:http://www.yihaomen.com/article/python/186.htm
用django template tag 的方式实现可以参考这篇文章:
http://www.yihaomen.com/article/python/339.htm
Django模版加法:
{{ value|add:10}}
value=5,则返回15 Django模版减法:
{{value|add:-10}}
value=5,则返回-5,这个比较好理解,减法就是加一个负数 Django模版乘法:
{% widthratio 5 1 100 %}
上面的代码表示:5/1 *100,返回500,widthratio需要三个参数,它会使用 参数1/参数2*参数3,所以要进行乘法的话,就将参数2=1即可 Django模版除法
{% widthratio 5 100 1 %}
上面的代码表示:5/100*1,返回0.05,只需要将第三个参数设置为1即可
---------------------
浙公网安备 33010602011771号