4

有没有办法在循环Qweb中从 python 字典中打印键和值?例如,如果我有一个返回字典的函数:

def get_informations(self):
    mydico={'myfirstkey':1,'mysecondkey':2}
    return mydico

然后,在 Qweb 报告中:

<t t-foreach="doc.get_informations()" t-as="l">
    <tr>
       <td class="text-right">
         <t t-esc="l"/>
       </td>
       <td class="text-right">
         <span t-esc="l_value"/>
       </td>
    </tr>
</t>

我如何打印

谢谢

2015 年 7 月 12 日更新:

谢谢你的回归。确切地说,当我把

 <t t-foreach="{'my': 'first', 'my2': 'second' }" t-as="v">

它有效,我有类似的东西:

my    first
my2   second

但是,当我在foreach中使用具有完全相同输出的函数时,qweb 无法将其分开,并且我有:

{'my': 'first', 'my2': 'second' }
{'my': 'first', 'my2': 'second' }

所以我决定换一种方式:

在我的继承报告中:

<t t-foreach="doc.tabTaxes" t-as="v">
    <tr>
        <td>
            <span t-esc="v.name"/>
        </td>
        <td>
            <span t-esc="doc.get_amount(v.name)[0]"/>
        </td>
    </tr>
</t>

sale.order模型中继承:

@api.one
def get_amount(self, taxeNom):
    total=0
    for ligne in self.order_line:
        for taxe in ligne.tax_id:
            if (taxeNom == taxe.name):
                try: total += ligne.price_reduce * (taxe.amount/100.)
                except: total +=0
    return "{0:.2f}".format(total)
4

4 回答 4

11

@FTK,

鉴于您的函数正在向 qWeb 模板返回有效的字典,下面的代码应该可以完成这项工作:

    <div id="wrap" class="oe_structure">
        <t t-foreach="{'my': 'first', 'my2': 'second' }" t-as="v">
         *<t t-esc="v"/> : <t t-esc="v_value"/></t>
    </div> 

您可以将 tr 放入循环中,这样它就会按照您的预期创建表格行,下面的代码将这样做:

    <div id="wrap" class="oe_structure">
        <table class="table table-bordered table-condensed">
            <t t-foreach="doc.get_informations()" t-as="item">
                <tr>
                    <td class="text-right">
                        <t t-esc="item"/>
                    </td>
                    <td class="text-right">
                        <t t-esc="item_value"/>
                    </td>
                </tr>
            </t>
        </table>
    </div>

您当然不需要 div 他们的要求。希望对你有帮助,

最好的,

于 2015-12-07T00:53:58.197 回答
7

最后我明白了如何使用 V9 :

<tr t-foreach="o.get_list_taxe(o.id)[0]" t-as="taxe">
  <t t-set="name" t-value="taxe['name']"/>
  <t t-set="total" t-value="taxe['total']"/>
  <td>
    <strong>
      <p>
        <t t-esc="name"/>
      </p>
    </strong>
  </td>
  <td class="text-right">
    <t t-esc="total" t-esc-options='{"widget": "monetary", "display_currency": "res_company.currency_id"}'/>
  </td>
</tr>
于 2016-02-26T15:19:26.960 回答
3

这对我有用,因为我发现当您遍历字典时,它会返回一个包含两个项目的元组(key, value)

<h4>Houses sold by type</h4>
  <t t-foreach="house_count" t-as="houses">
    <t t-set="house" t-value="houses[0]"/>
    <t t-set="count" t-value="houses[1]"/>
      <p><span t-esc="house" />: <span t-esc="count" /> houses sold</p>
  </t>

仅以房屋为例

于 2016-05-23T22:13:25.267 回答
3
  • $as_all

被迭代的对象

  • $as_value

当前迭代值,与列表和整数的 $as 相同,但对于映射,它提供值(其中 $as 提供键)

  • 警告

$as 将替换为传递给 t-as 的名称

此链接的参考:https ://www.odoo.com/documentation/8.0/reference/qweb.html

于 2015-12-07T13:04:08.350 回答