首页 \ 问答 \ 检查一个值是否等于Django模板中另一个数字的模数(Check if a value is equal to the modulo of another number in a Django template)

检查一个值是否等于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 something

idx 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 returns True so it is almost impossible to compare the number 2 after getting its modulo.


原文:https://stackoverflow.com/questions/35528897
更新时间:2022-05-16 20:05

最满意答案

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

相关问答

更多

相关文章

更多

最新问答

更多
  • 在页面树中使用不同的域(Use different domains in page tree)
  • 为什么这个半圈不会成长?(Why won't this semi circle grow?)
  • 如何学习计算机编程?
  • 如何在单个gcc命令中将多个头文件包和库目录添加到搜索路径?(How to add multiple header include and library directories to the search path in a single gcc command?)
  • VB.Net绑定datagridview Comboboxcolumn到datagridviewTextboxColumn(VB.Net binding datagridview Comboboxcolumn to datagridviewTextboxColumn)
  • 数据库设计 - 当一个记录有许多属性时(database design - when one record has many properties)
  • MATLAB函数计算两个坐标(纬度和经度)之间的距离(MATLAB function to calculate distance between two coordinates (latitude and longitude))
  • 如何从TFS中的团队项目查询所有PBI的历史变更?(How to query all PBI's historical changes from a team project in TFS?)
  • 使用点表示法的Python导入无法按预期工作(Python import using dot notation doesn't work as expected)
  • 我们能否将String列设置为partitionColumn?(Can we able to set String column as partitionColumn?)
  • 下划线模板无法按预期工作(Underscore template doesn't work as intended)
  • 怎么写项目开发的文档?
  • 为什么我的plpgsql函数没有返回任何行(why does my plpgsql function return no rows)
  • 补偿ARM中断的延迟?(compensating latency on ARM interrupts?)
  • Clojure:半平铺嵌套序列(Clojure: Semi-Flattening a nested Sequence)
  • Array方法返回所有null元素而不是object元素(Array method returning all null elements instead of object elements)
  • 禁止基于IP的用户,而不允许管理员访问IP(Ban user based on IP without allowing Admin to access IP)
  • c#wpf多重绑定不是很流行(c# wpf Multibindings not aviable)
  • 亚马逊Generic是什么意思
  • 如何在jgroup复制的hashmap中为状态转移提供超时?(How to give timeout for state transfer in jgroup replicated hashmap?)
  • highcharts可以生成这样的3d柱形图吗?(Can highcharts generate a 3d column chart like this?)
  • 在rails控制台中选择第二个条目?(Selecting second entry in rails console?)
  • 通过了解标记并且不循环遍历项目,将值设置为列表视图的某列中的行(Set a value to a row in a certain column of a listview by knowing the tag and without looping through items)
  • 杀死一个分叉的孩子(Kill a Forked Child)
  • 什么是代码点和代码空间?(What is a code point and code space?)
  • 如何在ruby中获取第一个html标记和第二个html标记之间的内容(how to get the content between the first html tag and the second html tag in ruby)
  • 需要在recyclerview中获取新添加项的视图参考(Need to get the view reference of newly added item in recyclerview)
  • 我今天部计算机出现故障啊,在按开机后,主机便发出“迪”的开机的报警声啊,在上午上过后就不行了,我只是把计算机的所有线重插过就这样,因为D线太乱啊,整理下。但到晚上计算机又能开机啊,可以详细告诉原因吗
  • 将字符串转换为NumPy datetime64 dtype(Convert string to NumPy datetime64 dtype)
  • 模块VB.Net中的命名空间(Namespace in module VB.Net)