How can I create a for-loop with a specific number of loops in Qweb?

I would like to make the cycle for printing elements the exact number of times. Something like that:

<t t-for="o.label_qty" >
...
</t>

Where o.label_qtyis an integer.

But I can only use a loop t-foreachin qweb:

<t t-foreach="o.pack_operation_ids" t-as="l" >
...
</t>

Is there any way to do this?

If I don't think the only solution is to create a dummy list with items o.label_qtyand write it to foreach.

+4
source share
3 answers

The directive t-foreachaccepts a Python expression. This way you can use the range()same as in Python loops for:

<t t-foreach="range(o.label_qty)" t-as="l">
...
</t>
+6
source

, Odoo Qweb Report -

     <t t-foreach="o.pack_operation_ids" t-as="l" >
         <td class="col-xs-1">
             <span t-esc="l_index+1"/>
         </td>
     </t>

, <span> tag no times, , qweb. index Qweb Template Engine, , 0 element.

, :)

+4
Function

range () will raise the error for a floating value.

For instance:

>>>a=1.0
>>>range(a)
>>>Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   TypeError: range() integer end argument expected, got float.

For dynamic loop variables, there are two possibilities for a loop with a specific number.

  • Integer (as @ Daniel Reis replied)
  • Float number (try with the following)

    <t t-set="i" t-value="int(o.label_qty)"/>
    <t t-foreach="range(i)" t-as="l">
    ...
    </t>
    

for more information on the range () function.

+2
source

Source: https://habr.com/ru/post/1598373/


All Articles