콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
1911 화면

Hi everyone,

I'm trying to include JavaScript inside an HTML field in Odoo 17, but the script doesn't seem to execute. Here's the code I'm working with:

Copy code

html_field = fields.HTML("HTML Data", sanitize=False, compute="_compute_html") def _compute_html(self): self.html_field = """<script>console.log('TEST')</script>"""

The HTML field is defined as a computed field, and I have set sanitize=False to allow raw HTML. However, when I load the view, the JavaScript inside the <script> tag does not run.

The reason I need this is to call a modal or wizard dynamically in my custom form template. The data for the form is fetched using custom logic with PostgreSQL queries, so I was hoping to inject the JavaScript for triggering the modal or wizard directly into the HTML field.

Does Odoo block scripts in HTML fields by design? If so, how can I achieve this functionality? Is there a recommended way to include JavaScript for modals or wizards in custom templates while still leveraging Odoo's framework?

Any guidance would be greatly appreciated!

아바타
취소
베스트 답변

Even though you've set sanitize=False on the HTML field, Odoo may still be applying some additional sanitization or escaping to the HTML content, which could prevent the script from running

To achieve the functionality you're looking for, you can consider use of an Odoo Widget with JavaScript

You can find the example from module im_livechat

script_external = fields.Html( 'Script (external)' , compute = '_compute_script_external' , store = False , readonly = True , sanitize = False )

def _compute_script_external(self):
values = {
"dbname": self._cr.dbname,
}
for record in self:


values["channel_id"] = record.id
values["url"] = record.get_base_url()
record.script_external = self.env['ir.qweb']._render('im_livechat.external_loader', values) if record.id else False
<template id ="external_loader" name ="Livechat : external_script field of livechat channel" > 
<!-- the loader -->
<script t-attf-src ="{{url}}/im_livechat/loader/{{channel_id }}" type ="text/javascript" />
<!-- js of all the required lib (internal and external) -->
<script t-attf-src ="{{url}}/im_livechat/assets_embed.js " type ="text/javascript" />
</template>
아바타
취소
관련 게시물 답글 화면 활동
0
8월 16
576
0
4월 25
789
1
1월 25
1147
0
3월 21
3183
1
8월 19
6207