3

I try to test custom widget from js reference and I get error in debugger:

Error: QWeb2: Template 'some.template' not found

qweb.xml was properly set in manifest, because when I extend ListController and use another template, it works correctly.

Here is template definition, which I use in qweb.xml:

<?xml version="1.0" encoding="UTF-8"?>
<template>
    <div t-name="some.template">
        <span class="val"><t t-esc="widget.count"/></span>
        <button>Increment</button>
    </div>
</template>

I tried to change <template> -> <templates>, totally removed tag "template" but still get the same error message.

JS:

odoo.define('working.test', function (require) {
var Widget = require('web.Widget');
var Counter = Widget.extend({
    template: 'some.template',
    events: {
        'click button': '_onClick',
    },
    init: function (parent, value) {
        this._super(parent);
        this.count = value;
    },
    _onClick: function () {
        this.count++;
        this.$('.val').text(this.count);
    },
});

// Create the instance
var counter = new Counter(this, 4);
// Render and insert into DOM
counter.appendTo(".o_nocontent_help");

})

Manifest:

# -*- coding: utf-8 -*-
{
    'name': "testwidget",

    'summary': """
        Short (1 phrase/line) summary of the module's purpose, used as
        subtitle on modules listing or apps.openerp.com""",

    'description': """
        Long description of module's purpose
    """,

    'author': "My Company",
    'website': "http://www.yourcompany.com",

    # Categories can be used to filter modules in modules listing
    # Check https://github.com/odoo/odoo/blob/12.0/odoo/addons/base/data/ir_module_category_data.xml
    # for the full list
    'category': 'Uncategorized',
    'version': '0.1',

    # any module necessary for this one to work correctly
    'depends': ['base'],
    'qweb': ['static/qweb.xml'],

    # always loaded
    'data': [
        # 'security/ir.model.access.csv',
        'views/views.xml',
        'views/web_asset.xml',
    ],
    # only loaded in demonstration mode
    'demo': [
        'demo/demo.xml',
    ],
}

Any idea how I need to modify this template to make the widget working correctly and in which table in db odoo stores these templates?

edward
  • 243
  • 3
  • 13

7 Answers7

1

You can try changing

'qweb': ['static/qweb.xml'],

to

'qweb': ['static/*.xml'],

It happens with me sometimes, by specifying static xml file name, it does not render that template. But by just loading all .xml files by using *, templates are loaded.

  • Thanks, I changed it according to your advise but still I'm getting the same error message. The whole code you can check on this repository: https://bitbucket.org/jarvee/test/src/master/ – edward Nov 10 '19 at 20:23
1

I was running into this same issue and needed to put my QWeb code into static/src/xml/base.xml in order for Odoo to recognize it.

You can check to see if Odoo is loading the QWeb by going to this URL on your Odoo instance:

<odoo_instance>/web/webclient/qweb?mods=<my_module_name>

Such as:

localhost:8069/web/webclient/qweb?mods=test

For comparison, you can see a successful output by using mods=web to load the QWeb assets for the web module.

travisw
  • 2,052
  • 1
  • 14
  • 27
  • When I open localhost:8069/web/webclient/qweb?mods=test I can see new qweb teamplate but still when loads js the log in console is: QWeb2: Template 'some' not found. Any idea why even if template is already visible and DB is restarted it's still not recognizable? – edward Nov 19 '19 at 17:05
  • Your template should be declared by `t` elements, not `div` elements. Your questions shows `
    ` instead of ``.
    – travisw Nov 19 '19 at 19:46
  • Another thing: if the error says `Template "some" not found` then I think you might be referencing `some` instead of `some.template` in your code. Normally, I would expect the error message to say `Template "some.template" not found` – travisw Nov 19 '19 at 19:47
  • I removed div and still the same error in console.log. Regarding 'some' I've just locally renamed a lot of things with a hope that it will refresh data. I added all changes in this rep: https://bitbucket.org/jarvee/test/src/master/ – edward Nov 19 '19 at 21:50
1

To solve this issue I used as workaround Widget.xmlDependencies:

 xmlDependencies: ['/test/static/qweb.xml']

but the main reason I think was cache in PyCharm which I didn't invalidate.

edward
  • 243
  • 3
  • 13
1

After having done some code reading, IMO, I realized the official documentation might not have pointed out clearly how to use templates in frontend.

To summarize my understanding:

The 'qweb' field in manifest is mainly designed for webclient (i.e. the backoffice), not the website. When entering webclient, a request to /web/webclient/qweb is made to retrieve all the templates of installed modules.

In order to use templates in website (i.e. frontend), synchronous and asynchronous ways both exist.

  • Synchronous way: Use qweb.add_template. When parameter is template content itself or a DOM node, template is loaded in a synchronous way. (While param is a URL, then it fires up an ajax request to server to fetch content.)

    qweb.add_template is mentioned in https://www.odoo.com/documentation/13.0/reference/qweb.html

  • Asynchronous way:

    1. Use ajax.loadXML which you can use anywhere you want to start loading template from a URL.
    2. Use xmlDependencies which you specify in widget definition. And if you dig into the code in widget.js, you can see ajax.loadXML is being used in willStart.

There are discussions regarding qweb.add_template vs ajax.loadXML See https://github.com/OCA/pylint-odoo/issues/186 and https://github.com/odoo/odoo/issues/20821

FYI.

ycl
  • 86
  • 1
  • 8
0

I guess you may need to make sure that the js definition refers to the module name correctly

odoo.define('MODULE TECHNICAL NAME SHOULD BE HERE.test', function (require) {});

you should also register your js function with something like:

core.action_registry.add("module_name.name", Widget_Extend);

for more info https://www.odoo.com/documentation/11.0/reference/javascript_reference.html#registries

kerbrose
  • 1,095
  • 2
  • 11
  • 22
  • Thanks, I changed it according to your advise but still I'm getting the same error message. The whole code you can check on this repository: bitbucket.org/jarvee/test/src/master – edward Nov 10 '19 at 20:23
  • @edward may i know whether you have resolved the issue? i have a similar situation here and am confused by the documentation. – ycl Aug 30 '20 at 12:28
  • @ycl yes, I used as a workaround xmlDependencies in that case – edward Aug 30 '20 at 18:31
0
        In Odoo 14 make sure
        dashboard.js
        odoo.define('library_managment.dashboard', function(require) {
            "use strict";
        
            // alert("hello odoo...............")
             console.log("Hello My Module........!!")
        
            var widgetRegistry = require('web.widget_registry');
            var Widget = require('web.Widget');
        
            var Counter = Widget.extend({
            template: 'library_managment.template',
            xmlDependencies: ['/library_managment/static/src/xml/template.xml'],
            events: {
                'click button': '_onClick',
            },
            init: function (parent, value) {
        
                this._super(parent);
                this.count = 4*9+5;
                 console.log("parent is", parent)
                 console.log("counter is..", this.count)
            },
            _onClick: function () {
                this.count++;
                this.$('.val').text(this.count);
            },
        
        });
        
            widgetRegistry.add('library_counter', Counter);
            return Counter;
        
        
        });
    
    template.xml
    add this 
    <?xml version="1.0" encoding="UTF-8"?>
    
    <odoo>
        <div t-name="library_managment.template">
            <span class="val">
                <t t-esc="widget.count"/>
            </span>
            <button class="bg-danger">Increment</button>
        </div>
    </odoo>


then add js file in assets.xml inside youe views
<odoo>
 <template id="assets_backend" name="Library assets" inherit_id="web.assets_backend">
            <xpath expr="." position="inside">
                <script type="text/javascript" src="/library_managment/static/src/js/dashboard.js"></script>
            </xpath>
        </template>
</odoo>

then add in manifest like this: 'js': ['/static/src/js/dashboard.js'], 'qweb': ['/static/src/xml/template.xml']

then inside form view add this line <widget="library_counter"/>

0

I had the same problem but with "hr_org_chart" template idk why everything works fine in another computer but in mine it returned this problem, I solved it by installing this module hr-org-chart

YASSINE
  • 100
  • 1
  • 5