Web Controller in Odoo V9
5 March, 2021 by
Web Controller in Odoo V9
| No comments yet

Odoo web controller provides the facility to create frontend modules which can be easily integrate with the backend modules.


Here backend is refers to the modules which provides functionalities like hr, sales management, purchase management , inventory, warehouse etc. These functionalities are mostly accessed by user with permission to access these modules.


Frontend modules refers odoo website functionalities like website, website sale, website blog etc. These modules creates a website pages for the public users with both static and dynamic contents.


In this post I am going to show you how we can create a frontend modules with custom webcontroller that will fetch the data related to company employees and display in the website custom page which is normally build using the odoo qweb template. For that first we need to create a standard odoo module. The structure of module should be like below.


|?? Module

|   |? controller

|   |    |? __init__.py

|   |    |?main.py

|   |? view

|   |    |?template.xml

|   |? data

|   |    |?data.xml

|   |? __openerp__.py

|   |? __init__.py


After creating an odoo module with given structure, add the below content to create a class in /controller/main.py file. File content should include classes, required import statements and methods to handle business logics.


from openerp import SUPERUSER_ID from openerp.addons.web import http from openerp.http import request class WebsiteDemo(http.Controller): @http.route(‘/employee’, type=’http’, auth=’user’, website=True) def display_employee_data(self): cr, context, pool = request.cr, request.context, request.registry hr_employee = pool.get(‘hr.employee’) hr_employee_ids = hr_employee.search(cr, SUPERUSER_ID, [], context=context) hr_employee_data = hr_employee.browse(cr, SUPERUSER_ID, hr_employee_ids, context=context) values = { ’employees’ : hr_employee_data } return request.website.render(“website.employee”, values)

The decorator @http.route used on the method with one required argument are meant to handle the request comes to the url which we have passed in argument.


You can add your business logic in the body of the method. Here I have added code to fetch all of the employees data. After that it will render the page with given page template id and values.


In order to handle data sent by controller method on the page, you must create a website page by adding following content inview/template.xml file.


<?xml version=”1.0″ encoding=”utf-8″?> <openerp> <data> <template name=”Employee Details” id=”website.employee” page=”True”> <t t-call=”website.layout”> <div id=”page”> <h3>Employee Details</h3> <table class=”table table-hover”> <thead> <tr> <th>Name</th> <th>Email</th> </tr> </thead> <tbody> <tr> <t t-foreach=”employees” t-as=”employee”> <tr> <td><t t-esc=”employee.name_related” /></td> <td><t t-esc=”employee.work_email” /></td> </tr> </t> </tr> </tbody> </table> </div> </t> </template> </data> </openerp>

Odoo website page uses Qweb template engine to render data send by the controller method. By using template directives attributes you can render data on website page. By this way you can create your own custom webcontroller for your dynamic web pages. You can also go through the attached module webcontroller_demo.zip for further reference. Thank you!


Sign in to leave a comment