将变量传递给模板

时间:2017-06-18 10:53:56

标签: openerp odoo-10

鉴于此模板:

<template id="minimal_layout_inherit" inherit_id="report.minimal_layout">
      <xpath expr="//head" position="inside">
             <link rel='stylesheet' href="my_module/static/src/css/mycss.css"/>
      </xpath>
</template>

如何从后端传递一个名为css_file的变量,以便可以将模板重新排列为:

<template id="minimal_layout_inherit" inherit_id="report.minimal_layout">
      <xpath expr="//head" position="inside">
             <link rel='stylesheet' t-att-href="css_file"/>
      </xpath>
</template>

我尝试过KabyR的解析器,适合我的情况。但是css_file变量不会传递给继承的minimal_layout:     class ParticularReport(models.AbstractModel):         _name =&#39; report.my_module.report_saleorder&#39;

    @api.model
    def render_html(self, docids, data=None):
        report_obj = self.env['report']
        report = report_obj._get_report_from_name('my_module.report_saleorder')
        docs = self.env[report.model].browse(docids)
        docargs = {
            'doc_ids': docids,
            'doc_model': report.model,
            'docs': docs,
            'css_file': "/my_module/static/css/sale_report.css",
            'css_file2': "my_module/static/css/sale_report.css",
        }
        print '** DEBUG render_html docargs'
        print docargs
        for doc in docargs['docs']:
            print 'doc.id'
            print doc.id
        return report_obj.render('my_module.report_saleorder', docargs)

鉴于我有自定义模块:

<template id="report_saleorder">
    <t t-call="report.html_container">
        <t t-foreach="docs" t-as="doc">
            <t t-call="my_module.report_saleorder_document" t-lang="doc.partner_id.lang"/>
        </t>
    </t>
</template>

将report.html_container作为使用minimal_template的模板(在我们想要变量的地方继承的模板)。

但是css_file在继承模板中无法使用

    <template id="report_saleorder_css" inherit_id="report.minimal_layout">
        <xpath expr="html/head/*[position() = last()]" position="after">
            <link rel="stylesheet" t-attf-href="my_module/static/css/sale_report.css"/>
            <link rel="stylesheet" t-att-href="css_file"/>
            <link t-att-href="'%s' % (css_file)" rel="stylesheet"/>
            <link t-att-href="'%s' % (css_file2)" rel="stylesheet"/>
            <span>AAA</span><span><t t-esc="css_file" /></span><span>BBB</span>
        </xpath>
    </template>

你得到了

2017-06-23 00:03:09,376 5131 INFO ? werkzeug: 127.0.0.1 - - [23/Jun/2017 00:03:09] "GET /my_module/static/css/sale_report.css HTTP/1.1" 200 -
2017-06-23 00:03:09,397 5131 INFO demo_v10 werkzeug: 127.0.0.1 - - [23/Jun/2017 00:03:09] "GET /None HTTP/1.1" 404 -
2017-06-23 00:03:09,963 5131 INFO demo_v10 werkzeug: 127.0.0.1 - - [23/Jun/2017 00:03:09] "GET /None HTTP/1.1" 404 -

正如你所看到的那样,第一个(硬编码)工作,第二个和第三个(使用css_file和css_file2变量)不工作。此外,文档中的AAA和BBB之间也没有任何内容。所以css_file,不存在。

1 个答案:

答案 0 :(得分:0)

尝试以下代码。 的 parser.py

class ReportSaleOrderInherit(models.AbstractModel):

_name = 'report.sale.report_saleorder'
@api.model
def render_html(self, docids, data=None):
    report = self.env['report']._get_report_from_name('sale.report_saleorder')
    total = []
    model = report.model
    docs = self.env[model].browse(self.env.context.get('active_id'))
    docargs = {
        'doc_ids': self.ids,
        'doc_model': report.model,
        # 'data': data['form'],
        'docs': docs,
        'css_file':"/pos_general/static/src/css/report_css.css",
    }
    return self.env['report'].render('sale.report_saleorder', docargs)

<强> TEMPLATE.XML

<template id="minimal_layout_inherit" inherit_id="report.minimal_layout">
  <xpath expr="//head" position="inside">
        <link t-att-href="'%s' % (css_file)" rel="stylesheet"/>
  </xpath>
</template>