<td>{% if item[5] is nan %}{% else %}{{item[5]}}{% endif %}</td>jinja2.exceptions.TemplateRuntimeError: No test named 'NaN' found.
时间: 2023-11-23 18:09:34 浏览: 27
这个错误是因为在Jinja2中没有名为`NaN`的测试。相反,你可以使用Python内置的`math.isnan()`函数来检查浮点数是否为NaN。
例如,你可以这样写模板:
```html
<td>{% if item[5] != item[5] %}{% else %}{{item[5]}}{% endif %}</td>
```
这里使用了`item[5] != item[5]`来检查`item[5]`是否为NaN。如果是NaN,则不执行if语句块,否则输出`item[5]`的值。
相关问题
<table class="tab_1"> <tr> <td><input type="checkbox" id="select-all"></td> <td>录入时间</td> <td>退货单号</td> <td>录入人</td> <td>备注</td> <td>状态</td> <td>日志</td> </tr> {%for item in data%} <tr onclick="goToLink('/show_tui?tuihuodanhao={{item[1]}}');"> <td id="cell1"><input type="checkbox" value="{{item[1]}}" name="id"></td> <td>{{item[0]}}</td> <td><a href="/do_chawuliu2?danhao={{item[1]}}">{{item[1]}}</a></td> <td>{{item[2]}}</td> <td>{{item[3]}}</td> <td>{% if item[4] is none %}{% else %}{{item[4]}}{% endif %}</td> <td >日志</td> </tr> {%endfor%} </table> <script> function goToLink(link) { window.location = link; } </script> <script> var cell = document.getElementById("cell1"); cell.onclick = null; </script> cell1不起作用
问题可能在于您的JavaScript代码中的位置。在您的示例代码中,您正在尝试使用JavaScript删除单元格中的onclick属性,但是该代码位于HTML表格之后。由于JavaScript代码在HTML代码之后加载和执行,所以当您尝试删除onclick属性时,该单元格可能还没有被加载到页面上,因此代码不起作用。
要解决这个问题,您应该将JavaScript代码放在HTML表格之前,或者在页面加载时使用window.onload事件来确保表格和单元格已经加载到页面上。以下是一个示例代码:
```
<script>
window.onload = function() {
var cell = document.getElementById("cell1");
cell.onclick = null;
}
</script>
<table class="tab_1">
<tr>
<td><input type="checkbox" id="select-all"></td>
<td>录入时间</td>
<td>退货单号</td>
<td>录入人</td>
<td>备注</td>
<td>状态</td>
<td>日志</td>
</tr>
{% for item in data %}
<tr onclick="goToLink('/show_tui?tuihuodanhao={{item[1]}}');">
<td id="cell1"><input type="checkbox" value="{{item[1]}}" name="id"></td>
<td>{{item[0]}}</td>
<td><a href="/do_chawuliu2?danhao={{item[1]}}">{{item[1]}}</a></td>
<td>{{item[2]}}</td>
<td>{{item[3]}}</td>
<td>{% if item[4] is none %}{% else %}{{item[4]}}{% endif %}</td>
<td>日志</td>
</tr>
{% endfor %}
</table>
```
在这个例子中,我们将JavaScript代码放在了window.onload事件处理程序中,以确保表格和单元格已经被加载到页面上。现在,当页面加载完成时,JavaScript代码会找到ID为“cell1”的单元格,并将其onclick属性设置为null,从而删除任何之前定义的onclick事件处理程序。
<td>{% if item[5] is nan %}{% else %}{{item[5]}}{% endif %}</td>
这是一个针对列表数据的条件语句,用于判断列表中第5个元素是否是 NaN(Not a Number,非数字)。如果是 NaN,则不做处理;否则,将该元素输出。其中,{% if %} 和 {% endif %} 是条件判断的语法,{% else %} 是可选的,表示当条件不成立时执行的代码块。{{ }} 用于输出变量的值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
ID
用户名
电话
公司
关心的产品
创建时间
建议
{% for item in objs %}
{{ item.id }}
{{ item.name }}
{{ item.get_gender_display }}
{{ item.create_time|date:"Y-m-d" }}
{{ item.get_status_display }}
{% if request.session.userid.gender == 0 %}
修改
删除
{% endif %}
{% for r in result1 %}
{% if r.mid %}
 
{{r.music.mname}}   
{{r.music.sname}}
{% endif %}
{% endfor %}
如何使第二里的内容右对齐?
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
{% for r in result1 %}
{% if r.mid %}
 
{{r.music.mname}}   
{{r.music.sname}}
{% endif %}
{% endfor %}
如何使第二里的内容右对齐?
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)