2013-06-26 92 views
0

我有以下tal條件代碼,理論上應該工作,但它不執行條件檢查。表單命中這個條件,運行它,然後返回除以0除法錯誤,而不是0tal:條件不執行條件檢查

我有意使用不完整的數據,這是爲了確保錯誤頁面不生成,表格中的單元格只是顯示一個0.

<td style="text-align: right;"> 
     <span tal:condition="python:result.sum_adt_out<>0"> 
       <span tal:replace="python:'%.1f'%((float(result.sum_cenmn) or 0)/float(result.sum_adt_out))">currentindex</span></span> 

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

如果任何人有任何想法,他們將不勝感激。

回答

0

如果您的result.sum_adt_out屬性是字符串,您的測試將失敗。

另請注意,在Python中已棄用<>,而是使用!=來測試不等式。您的模板,簡化並呼籲值float(),以確保它是數值,然後成爲:

<td style="text-align: right;" tal:define="sum_adt_out python:float(result.sum_adt_out)"> 
    <span tal:condition="sum_adt_out" 
      tal:content="python:'%.1f' % (float(result.sum_cenmn)/sum_adt_out,)">currentindex</span> 

    <span tal:condition="not:sum_adt_out">0.0</span> 

</td> 
+0

這很好地工作。非常非常感謝你! – John