Development Book V18: Controllers

In Odoo, a controller is a Python class responsible for managing the communication between backend logic (such as models) and frontend views (like web pages). It processes incoming HTTP requests from the browser, interacts with models or other components as needed, and returns the appropriate response to be rendered on the website.

To create a web controller in a custom module, start by creating a directory named controllers inside your module. Within this directory, include an __init__.py file and at least one Python file that will define your controller logic.

Here’s an example to illustrate the setup:

@http.route('/product', type='http', auth="user", website=True)
def product_details(self):
   product_id =request.env['product.template'].sudo().search([]])
   values = {
       'product': product_id,
   }
   return request.render('store.product_details', values)

In Odoo, controllers are created by inheriting from the Controller class, as shown in the example code. The @http.route() decorator is used to define key attributes of the route, such as the URL path, the type of request (either HTTP or JSON), authentication requirements, and the website parameter, which determines whether the route is linked to a website page (set to either True or False).

Inside the controller method, request.render() is used to render a specific template when the defined URL is accessed. This function specifies which QWeb template should be used for the response.

After defining the controller, the next step is to create the corresponding XML file that includes the QWeb template referenced in the controller logic.


<?xml version="1.0" encoding="utf-8"?>
<odoo>
   <record id="product_details" model="website.menu">
       <field name="name">Product</field> <!-- Name of the menu-->
       <field name="url">/product</field>
       <field name="parent_id" ref="website.main_menu"/>
   </record>
   <template id="product_details_template" name="Product Details">
       <t t-call="website.layout">
           <div class="oe_structure">
               <div class="container">
                   <div class="row">
                       <center>
                           <h3>Product Details</h3>
                       </center>
                       <div class="card-deck">
                           <t t-foreach="product" t-as="line">
                               <div class="card">
                                   <img t-attf-src="data:image/png;base64,{{line.image_1920}}"
                                        class="img-thumbnail"/>
                                   <span t-esc="line.name"/>
                                   <span t-esc="line.description"/>
                                   <span t-esc="line.list_price"/>
                               </div>
                           </t>
                       </div>
                   </div>
               </div>
           </div>
       </t>
   </template>
</odoo>

This is the process for creating a new controller in Odoo 18.

Additionally, Odoo allows you to inherit and extend existing controllers. For instance, if you intend to override the CustomerPortal controller defined in the portal module:

Begin by importing the controller class using the following syntax:

from odoo.addons.portal.controllers.portal import CustomerPortal

Here, portal refers to the controller file that contains the definition of the CustomerPortal class. Once this controller is imported, you can override its methods as shown in the following example:

class CustomerPortal(CustomerPortal):

   def _prepare_portal_layout_values(self):
       values = super(CustomerPortal, self)._prepare_portal_layout_values()
       return values

This outlines the approach for both creating a new controller and overriding an existing one in Odoo 18.

whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message