This question has been flagged
2 Replies
7395 Views

I search a lot in the web, read the odoo docs, but I can't figure it out how to add a button before "Create" and "Import" buttons. I found many solutions in other versions (v8, v9 and v10) but none works.

The result should look like this. And the buttons have access to python functions.

link to image: ibb.co/gACTSc

Avatar
Discard
Best Answer

Hi,

You can add a button and bind it with the python method using JS.

Ex:

Create an XML (button_template.xml) file under "/static/src/xml" and add following code:

<t t-name="my_custom_button">
    <button class="btn btn-primary btn-sm o_tree_custom_button" type="button">My Button</button>
</t>

Add above file in "__manifest__.py" in qweb section:

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

Now, create a JS file under "/static/src/js (button_render.js)" and register this JS in web asset:

<template id="button_render_js" inherit_id="web.assets_backend">
    <xpath expr="." position="inside">
        <script type="text/javascript" src="/my_module/static/src/js/button_render.js"/>
    </xpath>
</template>

Now, add following code in your JS file:

odoo.define('my_custom_module.my_custom_button', function (require) {
"use strict";

var core = require('web.core');
var ajax = require('web.ajax');
var ListView = require('web.ListView');
var Dialog = require('web.Dialog');
var web_client = require('web.web_client');

var _t = core._t;
var _lt = core._lt;
var QWeb = core.qweb;

ListView.include({
  render_buttons: function($node) {
  this._super($node)
  var self = this;
  if (this.$buttons && this.model == 'sale.order') {
  this.$GetIdsbutton = $(QWeb.render("my_custom_button", {'widget': this}));
  this.$GetIdsbutton.appendTo(this.$buttons);
  $(this.$buttons).find('.o_tree_custom_button').on('click', function() {
                // Following route / method will be called when button is clicked
  ajax.jsonRpc('/web/my_custom_method', 'call', {}).done(function() {
  self.reload_content()
  })
  });
  }
  },
})
});

Now, create a controller file (controllers/main.py) and add a route method using "/web/my_custom_method"

# -*- coding: utf-8 -*-
from odoo import http
from odoo.http import request

class CustomController(http.Controller):

  @http.route('/web/my_custom_method', type='http', method='POST',
  auth='user', csrf=False)
  def custom_method(self, **kw):
  # Your code goes here
        return True

Hope this will help you.

Let me know for any guidance.

Sudhir Arya
ERP Harbor Consulting Services
Skype:sudhir@erpharbor.com
Website: http://www.erpharbor.com
Avatar
Discard
Author

Thanks for your help. I follow all the intructions but got this error:

Uncaught Error: QWeb2: Template 'my_custom_button' not found

Make sure you have added XML file in __manifest__.py

Author

Heres is a copy of my __manifest__.py:

{'name': 'Makuku Application',

'data': [

# 'security/ir.model.access.csv',

# 'security/makuku_access_rules.xml',

'views/makuku_head.xml',

'views/makuku_menu.xml',

'views/makuku_forms.xml',

],

'depends': ['base','web'],

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

'application': True}

Should be working fine as it is already working for me.

Try to remove following description from the JS file:

# Following route / method will be called when button is clicked

Best Answer

Hi,

You can add a new button near the Create and Import button like this,



Thanks

Avatar
Discard

How can you put function for this button? Please help me

How can you put function for this button? Please help me