Pergunta

I have the following tal condition code that in theory is supposed to work, but it is not performing the condition check. the form hits this condition, runs it and then returns a divide by 0 division error instead of a 0

Im working with incomplete data on purpose, this is to make sure that error pages dont generate, the cell in the table just shows a 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>

if anyone has any ideas, they would be much appreciated.

Foi útil?

Solução

If your result.sum_adt_out attribute is a string, your test will fail.

Also note that <> has been deprecated in Python, use != instead to test inequality. Your template, simplified and with calling float() on the value first to ensure that it is numeric, then becomes:

<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>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top