Development Book V17: Controllers

In Odoo, a controller is a Python class that manages the interactions between the backend logic (models) and the frontend views (web pages). Controllers handle incoming requests from the user's browser, process the requests by interacting with models or other components, and generate responses to be displayed on the web pages.

To create a web controller, begin by establishing a directory named 'Controllers' within our custom module. This directory should contain an init.py file and a Python file.

Let's look at an example.


from odoo import http

@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('product_details_template', values)

Controllers in Odoo are inherited from the "Controller" class, as evident from the provided code snippet. The @http.route() decorator within the controller defines various details, including the URL to redirect, request type (HTTP or JSON), authentication parameters to specify access, and the 'website' parameter to link the controller with a page (accepting True or False values).

Moreover, within the controller function definition, utilizing request.render() allows us to specify the template for rendering when redirecting to the designated URL.

Subsequently, we can generate the XML file responsible for creating the template as specified in the controller.


    <?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 how we create a new controller in Odoo 17.

In Odoo, inheriting an existing controller is also feasible. For example, if we aim to override the controller "CustomerPortal" defined by the "portal" module:

First, import the controller class as follows,

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

In this case, the "portal" refers to the controller file containing the "CustomerPortal" Controller class. Upon importing the controller, we can proceed to override the function as shown below,

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

This is the method for creating and overriding a controller in Odoo 17.

whatsapp
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