生成 html 并在 qweb 中渲染

use*_*doo 2 odoo odoo-9 odoo-10

是否可以在 .py 文件中生成 html 并在 qweb 中渲染?

<openerp>
    <data>
        <record id="paperformat_time" model="report.paperformat">
            <field name="name">Time</field>
            <field name="font_size">10</field>
        </record>

        <report id="time_qweb" model="hr_timesheet_sheet.sheet" string="Time" 
        report_type="qweb-pdf" name="time.report_time" file="time.report_time" />

        <record id="time_qweb" model="ir.actions.report.xml">
            <field name="paperformat_id" ref="time.paperformat_time" />
        </record>
      </data>
</openerp>

qweb    

<template id="report_time">
    <t t-call="report.html_container">
        <t t-foreach="docs" t-as="t">
            <span t-esc="t.__compute_html()" />
            <div class="page">
                <span t-field="t.html_text " />
            </div>
        </t>
    </t>
</template>
Run Code Online (Sandbox Code Playgroud)

.py 文件

class Time(models.Model):

   _inherit = 'hr_timesheet_sheet.sheet'

   html_text = fields.Html(string = 'Html')

   @api.one
   def _compute_html(self):
        html_value = "<h1>TEST</h1>" 
        html_value += "<h1>TEST 2</h1>"

        self.html_text = html_value
Run Code Online (Sandbox Code Playgroud)

例如。

html_value = "<h1> + employee_id.name + "</h1>" 
html_value += "<h1> + employee_id.phone + "</h1>"
Run Code Online (Sandbox Code Playgroud)

现在我需要在 qweb 中渲染 html_value 并放入<div class="page"> put here html_value </div>

现在我将文本保存在数据库中,有更好的解决方案吗?................................

Cha*_* DZ 6

是的,如果您有一个包含 html 代码的变量,t-esc或者t-fieldodoo 会将其打印为文本,则可以。

如果你想渲染它,请使用。 t-raw

  <div t-raw="doc.some_attribute" > </div>
Run Code Online (Sandbox Code Playgroud)

或者

   <t t-raw="doc.some_attribute" > </t>
Run Code Online (Sandbox Code Playgroud)