Adding static resources (CSS and JavaScript) to Odoo
Hi guys,
In this tutorial I will learn you how to add CSS and JavaScript to your custom Odoo module. This gives you the ability to change the design of things in Odoo, to execute JavaScript (and jQuery) and opens up a whole new set of possibilities to add extra options in Odoo.
In this example I will learn you how to add new JS/CSS files to Odoo and how they should be added to Odoo by the official guidelines. At the end you can also find a link to a demo module so you can always look at a code example too!

Now the first thing to do is to tell __openerp__.py that you’ve added this new XML file, otherwise Odoo will not load it. Open up __openerp__.py and add in a line in the data block. Be sure to give it the correct path and filename:

Now return to your XML file. All CSS and JavaScript in Odoo is added to the assets so the first step is to create an inherit_id calling this, then we’ll use an xpath to add in our custom coded files. Adding resources is much like adding a field to a view with inheritance, you’re just referring to a file and not a field. So how does this look in code?
Let us break this down! We’ve created a new template called
‘assets_backend’, which has a name ‘static_resource_demo assets’ and
which inherits ‘web.assets_backend’. The ‘web.assets_backend’ is already
created by Odoo and contains all the CSS/JavaScript code so we’ll
simply inherit it to add our files to it. The xpath expression is simply
telling Odoo let us add those files to the main path of
‘web.assets_backend’. Simple, isn’t it?

So in my example I’ve created a custom module named ‘static_resources_demo’ in which I created the folders. From here on you can add all the CSS in your CSS file as you like!
This leaves us with one more thing to do. Odoo doesn’t know the path to the CSS file yet so open up your XML file again (in my case named resources.xml) and add the path to your CSS file:
An important note is that you should also add the name of your module
in! The xpath and template know the path up in to the folder addons but
from there on Odoo is lost if you don’t tell it where to look. So first
add your module name and then continue with
/static/src/css/yourFileName.css.

So in my example I’ve created a custom module named ‘static_resources_demo’ in which I created the folders. From here on you can add all the JS in your JS file as you like!
This leaves us with one more thing to do. Odoo doesn’t know the path to the JS file yet so open up your XML file again (in my case named resources.xml) and add the path to your JS file:
Has this tutorial helped you, do you have any feedback or questions? Post away!
In this tutorial I will learn you how to add CSS and JavaScript to your custom Odoo module. This gives you the ability to change the design of things in Odoo, to execute JavaScript (and jQuery) and opens up a whole new set of possibilities to add extra options in Odoo.
In this example I will learn you how to add new JS/CSS files to Odoo and how they should be added to Odoo by the official guidelines. At the end you can also find a link to a demo module so you can always look at a code example too!
1. Creating the XML
The first step is to create a new XML file in your custom module. Create a new XML file under yourModuleName/views, like this:
Now the first thing to do is to tell __openerp__.py that you’ve added this new XML file, otherwise Odoo will not load it. Open up __openerp__.py and add in a line in the data block. Be sure to give it the correct path and filename:

Now return to your XML file. All CSS and JavaScript in Odoo is added to the assets so the first step is to create an inherit_id calling this, then we’ll use an xpath to add in our custom coded files. Adding resources is much like adding a field to a view with inheritance, you’re just referring to a file and not a field. So how does this look in code?
2. Creating your CSS / JavaScript files
Alright so now that we have the basis done for adding in CSS or JavaScript files we should first create them. The guidelines of Odoo say that you should create a new folder named ‘static’, so do so. Open up this folder and create a new folder ‘src’. After you’ve created the folder ‘src’ you should create a new one named ‘css’, ‘js’ or ‘images’ depending on which file you need to add. In this example I will explain both. If you want to add CSS files read part 2.1, if you want to add JavaScript files read part 2.2 and if you want to do both, well read both.2.1 Creating your CSS files
Create a new folder ‘css’ for this example and open it up. This is now the official structure for adding CSS files to Odoo! Now create a new CSS file in this folder. I’ve named it ‘cssfile.css’ just for demo purposes, but you can name this whatever you like. The final result will now look like this:
So in my example I’ve created a custom module named ‘static_resources_demo’ in which I created the folders. From here on you can add all the CSS in your CSS file as you like!
This leaves us with one more thing to do. Odoo doesn’t know the path to the CSS file yet so open up your XML file again (in my case named resources.xml) and add the path to your CSS file:
2.2 Creating your JavaScript files
Create a new folder ‘js’ for this example and open it up. This is now the official structure for adding JavaScript files to Odoo! Now create a new JS file in this folder. I’ve named it ‘javascriptfile.js’ just for demo purposes, but you can name this whatever you like. The final result will now look like this:
So in my example I’ve created a custom module named ‘static_resources_demo’ in which I created the folders. From here on you can add all the JS in your JS file as you like!
This leaves us with one more thing to do. Odoo doesn’t know the path to the JS file yet so open up your XML file again (in my case named resources.xml) and add the path to your JS file:
3. Conclusion
Adding CSS or JavaScript to your custom module is very easy and has a lot of possibilities. This is ideal if you want to change the layout of your Odoo, if you want to create another theme or if you want to have extra control with JavaScript. Adding extra resources gives you almost unlimited possibilties such as creating new widgets or adding external JS libraries in!Has this tutorial helped you, do you have any feedback or questions? Post away!
5 comments:
Great blog. Inspired me a lot. Thank you.
Software Testing Course in Madurai
Software Testing Classes in Madurai
Software Testing Training in Madurai
Software Testing Course in Coimbatore
Best Software Testing Training Institute in Coimbatore
Software Testing Institute in Coimbatore
Got to know something new reading your blog and thanks for sharing this with us. Great reading your blog.
Spoken English Class in Thiruvanmiyur
Spoken English Classes in Adyar
Spoken English Classes in T-Nagar
Spoken English Classes in Vadapalani
Spoken English Classes in Porur
Spoken English Classes in Anna Nagar
Spoken English Classes in Chennai Anna Nagar
Spoken English Classes in Perambur
Spoken English Classes in Anna Nagar West
Excellent blog!!! I got to know the more useful information by reading your blog. Thanks for posting this blog.
English Speaking Classes in Mulund
English Speaking Classes in Mulund West
English Speaking Course in Mulund
English Speaking Course in Mulund East
English Speaking Course in Mulund West
Best English Speaking Classes in Mulund West
English Speaking Classes Mulund
Very useful information, Keep posting more blog likes this, Thank you.
Aviation Courses in Chennai
cabin crew training institute in Chennai
airlines training Chennai
Ground staff training in Chennai
Aviation Academy in Chennai
air hostess training in Chennai
airport management courses in Chennai
ground staff training in Chennai
Good way of expressing your ideas with us. Thanks for sharing with us and please add more information's.
DOT NET Training in Chennai
.net course in chennai
c# training in chennai
Dot Net Training in Porur
Html5 Training in Chennai
html course fees in chennai
QTP Training in Chennai
LoadRunner Training in Chennai
Post a Comment