tal:条件参数用sid同时打印两个语句

2024-09-28 13:37:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图有两个Tal条件语句,检查代码是否是两件事之一,然后执行相应的计算。但是它会同时打印出两个条件的结果。你知道吗

谢谢!你知道吗

Unit    4.5522 0.0000
unit.. . 3.7628 0.0000
Unit     0.0000 14.6083
unit     0.0000 31.9430
<td style="text-align: right;">
  <span tal:condition="python:float(result.totdirrn)!=0 and (result.wkld1_desc!='Proceedures' and result.wkld1_desc!='Visits')">
  <span tal:replace="python:'%.4f'%(float(result.cenmn)/((((float(result.dirhrs)*(float(float(result.totdirrn)/float(result.dirhrs))))/14)/12)/2))">currentindex</span>
  <span tal:condition="python:float(result.totdirrn)!=0 and (result.wkld1_desc!='Census')">
  <span tal:replace="python:'%.4f'%(float(result.vipr)/((((float(result.dirhrs)*(float(float(result.totdirrn)/float(result.dirhrs))))/14)/12)/2))">currentindex</span></span>

  </span>
  <span tal:condition="python:(float(result.totdirrn)==0)">
  <span tal:replace="python:'%.1f'%(0.0)"></span></span>
</td>

Tags: andunitresultfloatcondition条件descreplace
1条回答
网友
1楼 · 发布于 2024-09-28 13:37:13

你想要做什么,或者你期望的结果是什么,这是相当不清楚的。我已经解决了您的模板代码的一般问题,让我们看看这是否解决了您的问题:

似乎你的开始和结束<span/>标签搞混了。当前您的结构如下所示:

<span condition>
    <span replace></span>
    <span condition>
        <span replace></span>
    </span>
<span>
<span condition>
    <span replace></span>
</span>

我怀疑你真的想:

<span condition>
    <span replace></span>
<span>
<span condition>
    <span replace></span>
</span>
<span condition>
    <span replace></span>
</span>

您可以将条件范围与tal:content属性组合起来,而不是替换嵌套的<span>标记,形成:

<span condition content></span>
<span condition content></span>
<span condition content></span>

或者,应用到您的示例代码中,进行一些改进,使其更具可读性:

<td style="text-align: right;"
      tal:define="desc result/wkld1_desc;
                  totdirrn python:float(result.totdirrn);
                  cenmn python:float(result.cenmn);
                  dirhrs python:float(result.dirhrs);
                  vipr python:float(result.vipr);
                 ">
  <span tal:condition="python:totdirrn and desc not in ('Proceedures', 'Visits')"
        tal:content="python:'%.4f' % (cenmn / (dirhrs * (totdirrn / dirhrs) / 336))">currentindex</span>
  <span tal:condition="python:totdirrn and desc != 'Census'" 
        tal:content="python:'%.4f' % (vipr / (dirhrs * (totdirrn / dirhrs) / 336))">currentindex</span>
  <span tal:condition="python:not totdirrn">0.0</span>
</td>

您可能需要确保那些float结果已经是浮点值,并避免视图中所有的float()调用。你知道吗

还要考虑到浮点值可能不是0,如果要求逗号后面的精度为4位,则0.000001的值不等于0,而是打印为0.000。你知道吗

相关问题 更多 >

    热门问题