Web Controller in Odoo

Odoo web controller has a facility that can create frontend modules integrate with the backend module.

Presently in Odoo, backend modules are accounting, CRM, discuss, helpdesk, hr, sales management, purchase management, inventory, warehouse, etc. These modules can be accessed by a user if a user is having permission to access these modules.

Frontend modules are those which refer to Odoo website functionalities likewise a website, website sale, website blog, etc. With the help of these modules, we also can create website pages with both static and dynamic content.

 

Now we will see how we can also create frontend modules with custom web controller that will fetch the data related to company departments and display in the website custom page which we can build using the Odoo web template.

So for this firstly we will create Odoo modules. The structure for the module is given below.

1]Module-

a]controller – __init__.py ,main.py

b]view – template.xml

c]data – data.xml

d]__openerp__.py

e]__init__.py

So from a structure, we have to create an Odoo module, after that add the below content to create a class in the main.py file.

Basically, the file includes the following content-

1]Classes

2]Required import statements

3]Methods to handle business logic

from OpenERP import SUPERUSER_ID

 openerp.addons.web import HTTP

from OpenERP.http import request

class Website_Demo(http.Controller):

@http.route('/department', type='http', auth='user', website=True)

def display_department_data(self):

cr, context, pool = request.cr, request.context, request.registry

hr_department = pool.get('hr.department ')

hr_department_ids = hr_department .search(cr, SUPERUSER_ID, [], context=context)

hr_department_data = hr_department .browse(cr, SUPERUSER_ID, hr_department_ids, context=context)

values = {

'department' : hr_department_data

}

return request.website.render("website.department ", values)

The @http.route is a decorator used on the method with one required argument. It is meant to handle the request that comes to the URL which we have passed in the argument.

In the body of a method, you can also add your business logic. Here I have added code to fetch all of the department data. After that, it will render the page with the given page template id and values.

You must create a website page in order to handle data sent by the controller method on the page. Similarly, on the website page, you have to add the following content to the template.XML file.

<?xml version="1.0" encoding="utf-8"?>

<openerp>

<data>

<template name="Department Details" id="website.department " page="True">

<t t-call="website.layout">

<div id="page">

<h3> Department Details</h3>

<table class="table table-hover">

<thead>

<tr>

<th>Name</th>

<th>Employee</th>

</tr>

</thead>

<tbody>

<tr>

<t t-foreach="departments" t-as="department">

<tr>

<td><t t-esc="department .name_related" /></td>

<td><t t-esc="department .working_employee" /></td>

</tr>

</t>

</tr>

</tbody>

</table>

</div>

</t>

</template>

</data>

</openerp>

Now website page uses the Qweb template engine to render data send by the controller method.

Finally, By using the template directives attribute you can render data on a website page. This way you can create your own custom web controllers. 

If you have any queries or looking for custom web controllers, please feel free to contact us

Leave a Reply

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.