【问题标题】:Generate html and render in qweb生成html并在qweb中渲染
【发布时间】:2018-06-07 21:27:49
【问题描述】:

是否可以在 .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>

.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

例如。

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

现在我需要在 qweb 中渲染 html_value 并放入 &lt;div class="page"&gt; put here html_value &lt;/div&gt;

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

【问题讨论】:

  • 如果你真的想在 .py 文件中“渲染” html_value,那么只需将它添加到 cotnroller request.aprams['html_value'] = html_value 然后在模板中
  • @TadeuszKarpinski 有什么简单的例子吗?

标签: odoo odoo-10 odoo-9


【解决方案1】:

是的,如果你有一个包含 html 代码的变量,如果你使用 t-esct-field,你可以,odoo 会将它打印为文本。

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

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

或者

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

【讨论】:

  • Tnx 求助,有没有更详细的例子。总帐
  • Xhat 你的意思是举例。你可以把你的代码放在问题中。如何计算价值。
  • 我在上面添加了我的示例,一切正常,但现在我将 html 文本保存在数据库中,并且该文本显示在 qweb 中。没有在数据库中保存文本的任何更好的解决方案。
  • 是的,首先我认为我应该接受我的回答。而不是将结果保存在字段中,而是直接在函数retun value_html 中返回它并使用 api.multi 因为 api.one 返回的结果将是一个列表。然后你可以直接调用你模板中的函数&lt;t t-raw="doc.compute_html()" /&gt;记得使用api.multi。更轻松
  • 并通过删除“_”来公开您的方法,因为我认为 odoo 会阻止您调用它。如果不只是保持它那样
【解决方案2】:

你可以试试这个

<span t-raw="my_html_field"/>

这里 my_html_field 是您的 html 格式数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-22
    • 2010-09-18
    • 1970-01-01
    • 2019-06-14
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    相关资源
    最近更新 更多