是否可以在.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中在put in <div class="page"> put here html_value </div>中进行<div class="page"> put here html_value </div>呈现。
现在我将文本保存在数据库中,还有更好的解决方案吗?.
发布于 2018-06-08 08:43:53
是的,如果您有一个包含html代码的变量,如果您使用t-esc或t-field,odoo会将它打印为文本。
如果你想让它使用。t-raw
<div t-raw="doc.some_attribute" > </div>或
<t t-raw="doc.some_attribute" > </t>发布于 2018-10-23 05:28:00
你可以试试这个
<span t-raw="my_html_field"/>这里,my_html_field是html格式的数据。
发布于 2022-08-09 07:20:37
由于Odoo版本15,t-raw是不可取的。您需要使用Python以安全的方式呈现HTML,然后将其解析为XML。参考文献
import markupsafe
...
class YourController(http.Controller):
...
@http.route(...)
def your_rendering_method(self):
...
return request.render("YOUR_TEMPLATE", {"YOUR_FIELD": markupsafe.Markup("<a href='https://www.stackoverflow.com'>Stack Overflow</a>")})<t t-esc="YOUR_FIELD" />https://stackoverflow.com/questions/50750410
复制相似问题