odooodoo-10odoo-9

Generate html and render in qweb


Is it possible generate html in .py file and render in 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>

.py file

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

eg.

html_value = "<h1> + employee_id.name + "</h1>" 
html_value += "<h1> + employee_id.phone + "</h1>"

now I need html_value render in qweb in put in <div class="page"> put here html_value </div>

Now I save text in database, any better solution?................................


Solution

  • Yes you can if you have a variable that have html code if you use t-esc or t-field odoo will print it as text.

    If you want to render it use. t-raw

      <div t-raw="doc.some_attribute" > </div>
    

    Or

       <t t-raw="doc.some_attribute" > </t>