检查一个值是否等于Django模板中另一个数字的模数(Check if a value is equal to the modulo of another number in a Django template)
我正在试图在Django中获得相同的结果
if idx % 3 == 2:
做些什么idx是forloop中的索引,但我简化了代码
我用这个:
{% ifequal idx|divisibleby:3 2 %} do something {% endifequal %}
似乎
divisibleby
过滤器返回True
所以在得到模数之后比较数字2几乎是不可能的。I am trying to get an equivalent of this in Django
if idx % 3 == 2:
do somethingidx is an index in a forloop but I have simplified the code
I used this:
{% ifequal idx|divisibleby:3 2 %} do something {% endifequal %}
It seems like the
divisibleby
filter returnsTrue
so it is almost impossible to compare the number 2 after getting its modulo.
原文:https://stackoverflow.com/questions/35528897
满意答案
DivisibleBy只是做一个模数来检查最终值是否等于0,没有任何东西阻止你使用完全相同的代码制作自己的模板标签而没有最终检查
@register.filter(is_safe=False) def modulo(value, arg): return int(value) % int(arg) {% ifequal idx|modulo:3 2 %}
DivisibleBy just does a modulo to check whether or not the final value is equal to 0, there isn't anything stopping you making your own template tag using the exact same code without the final check
@register.filter(is_safe=False) def modulo(value, arg): return int(value) % int(arg) {% ifequal idx|modulo:3 2 %}
相关问答
更多调用render()时如何获取Django模板错误的文件名和行号?(How to get the filename and line number of a Django template error when calling render()?)
Modulo处理不正确(Modulo is not processing correctly)
检查一个值是否等于Django模板中另一个数字的模数(Check if a value is equal to the modulo of another number in a Django template)
django模板从字母数字中获取数字(django template to get number from alphanumeric)
模数为双打(Modulo for doubles)
Django模板中具有相同位置的编号列表?(A numbered list with some equal positions in a Django template?)
检查模数1(checking number modulo 1)
在django模板上显示等于持续时间值的(时间)图标(displaying (time )icons equal to duration value on django template)
相关文章
更多vicalloy的我与django
Java Number类
Drupal Forums instead of phpBB or vBulletin: A casestudy
django-haystack+solr实现搜索
在django中加入搜索引擎
《Joomla 2.5 模板教程:宁皓网》(Create a Joomla 2.5 Template)前两章 + 模板资源[光盘镜像]
Django资源汇总(转)
Haystack - Search for Django
Django下载及安装
Becoming a data scientist
Copyright ©2018 656463.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!