![](/img/trans.png)
[英]How to set JS variable value into an Odoo qweb template variable value
[英]Qweb template not found - Odoo v8
我正在使用 Odoo v8 開發一個網站。 我想寫一個片段,它的結構是由 javascript 加載的。 波紋管是我的代碼......首先,我有一個片段結構:
<template id="snippet_hello" inherit_id="website2.snippets" name="Snippet Hello">
<xpath expr="//div[@id='snippet_structure']" position="inside">
<div class="oe_snippet">
<div class="oe_snippet_thumbnail">
<img class="oe_snippet_thumbnail_img" src="/path_to_block_icon/block_icon.png"/>
<span class="oe_snippet_thumbnail_title">Hello</span>
</div>
<section class="oe_snippet_body">
<div class="oe_snippet_hello">Hello ...</div>
</section>
</div>
</xpath>
<xpath expr="//div[@id='snippet_options']" position="inside">
<div data-snippet-option-id='snippet_hello'
data-selector=".oe_snippet_hello"
data-selector-siblings="p, h1, h2, h3, blockquote, .well, .panel">
</div>
</xpath>
</template>
然后我有一些 javascript 代碼來呈現片段內容:
(function () {
'use strict';
var website = openerp.website;
qweb = openerp.qweb;
qweb.add_template('/path_to_snippet_qweb_template/snippet_template_filename.xml');
website.snippet.animationRegistry.hello = website.snippet.Animation.extend({
selector: ".oe_snippet_hello",
start: function(){
var $content = $(qweb.render('website.snippet_hello', {a:1}));
$content.appendTo(this.$target);
},
});
})();
然后我有一個 QWeb 模板來顯示我的結構內容(文件名:snippet_template_filename.xml):
<?xml version="1.0" encoding="utf-8"?>
<templates id="template" xml:space="preserve">
<t t-name="website.snippet_hello">
<div contenteditable="false">
<p>Hello snippet</p>
<t t-esc="a"/>
</div>
</t>
</templates>
問題是這一行:
var $content = $(qweb.render('website.snippet_hello', {a:1}));
出現“找不到模板‘website.snippet_hello’”的錯誤我注意到當我以管理員身份登錄時(沒有嘗試過其他帳戶),它運行良好。 當我在瀏覽器上注銷時,它只是發生了錯誤。 請給我您的建議,謝謝!
這是一個與過時的 Odoo 版本相關的老問題,但今天的答案仍然相關(Odoo v11/12/13):
Template Not found
可能在以下情況下發生:
通常,您將項目中的模板保存為/your_module/static/src/xml/snippet_template_filename.xml
,並且您必須通過添加以下內容將這個 xml 文件加載到/your_module/__manifest__.py
:
'qweb': [
"static/src/xml/snippet_template_filename.xml",
],
或簡寫:
'qweb': [
"static/src/xml/*.xml",
],
您在 odoo 的 App 菜單中安裝/更新 your_module,然后您可以通過查看http://localhost:8069/web/webclient/qweb?mods=your_module
來驗證您的模板是否已加載,它應該返回您的模板。
您還可以查看最喜歡的瀏覽器網絡檢查器來檢查http://localhost:8069/web/webclient/qweb?mods=[...]
請求並檢查 mods your_module
是否正確加載。
模板可以像這樣在你的 js 中使用(Odoo >= v11):
odoo.define('your_module.NameOfYourJs', function (require) {
"use strict";
var QWeb = core.qweb;
[...]
var result = QWeb.render('website.snippet_hello', {a:1});
});
注意:要調試資產,您可以使用http://localhost:8069/web?debug=assets
。
希望這有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.