How to Create & Configure Web
Controllers in Odoo 18
Enterprise
Enterprise
Introduction
In this we’ll discuss on how to create & configure web controllers
in Odoo 18. In Odoo, the Front-end modules under "Website" are
configured using "Controllers". These front-end modules and back-
end modules are connected. We can define the URL to link the Web
Pages using Controllers. In contrast to models, controllers offer their
own extension method.
Enterprise
We must first establish a folder called "controllers" in our custom module in order to
work with controllers. Init files and python files like models can be found in the
Controllers folder. In contrast to models, controllers thus offer their own extension
mechanism: Creating controllers involves deriving from the Controller class.
Methods annotated with route() are used to define routes. We can create the
controller using the sample code:
from odoo import http
from odoo.http import request
class ControllerName(http.controller):
@http.route(['/url/'], type="http", auth="public",
website="True")
def function_name(self, **post):
Variable =
request.env['models_name'].sudo().search([ ])
values = {
'records': Variable
}
return request.render("module_name.template_id",
values)
Enterprise
To load the controller, we must first import the libraries, "http" and
"request." Routes are defined through a method decorated with
route(). Here we can see ‘@http.route()’ allows us to navigate to
specific pages. And it also helps to link the URL with a specific
webpage.
Inside decorator, we can also use some keywords like;
● ‘/url’: Need to specify the ‘url’ to be used to redirect a specific
page
● ‘type’: Specify the type of request. There are two types:
a) ‘json’: This request is more suitable for executing methods on
the server and obtaining results.
b) ‘http’: This is simpler and easier to use when we want to
access some resources on the server.
Enterprise
● ‘auth’: It’ defines the access to the URL or to view the webpage
related to the link.
a) ‘user’: If auth is the user, the current request will perform
only for the user.
b) ‘public’: If auth is public, the current request will perform for
all the users. There will be no restrictions.
c) ‘none’: If auth is none, nobody can access the request and
database.
● ‘website’: We can mention that the controller is linking to the
webpage by setting the value as ‘True’ or ‘False’.
Enterprise
● ‘methods’: There are two methods. we can specify one, If not,
all methods are allowed.
a) [‘GET’]: GET is used for viewing something, without changing
it,
b) [‘POST’]: POST is used for changing something.
● ‘sitemap’: It Serves as the directory for website pages
accessible to users.
● ‘multilang’: Used to add languages to your website and
translate it from the frontend.
● ‘csrf’: It’s used for secure user sessions by generating a token.
Enterprise
Now we can render the web page containing data, using
This is for rendering the webpage.’request.render’ helps to render
the webpage from the controller. The webpage template is defined
by module_name.template_id. If you need to redirect to any another
URL instead of rendering a web page, we use ‘request.redirect’. For
example, if you need to redirect to ‘shop’,
return request.render("module_name.template_id",
values)
return request.redirect('/shop')
Enterprise
Creating the View
We can now construct the template, whose XML name should be
"template_id" under the module's "views" subdirectory. since the view
was previously invoked from the controller.
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="template_id" name="Name">
<t t-call="website.layout">
<div class="oe_structure">
<div class="container"><br />
<center>
<h3>Headline</h3>
</center><br />
<table class="table-striped table">
<thead style="font-size: 23px;">
<tr>
<h6><b>
<th>Header1</th>
Enterprise
<th>Header 2</th>
</b></h6>
</tr>
</thead>
<tbody>
<t t-foreach="records" t-as="order">
<tr>
<td><span t-
esc="order.value1" /></td>
<td><span t-
esc="order.value2" /></td>
</tr>
</t>
</tbody>
</table>
</div>
</div>
</t>
</template>
</odoo>
Enterprise
We can also override the current controller function in Odoo to suit
our needs. It’s not the same as overriding in other python files. As an
illustration, suppose we need to alter the controller function from the
CustomerPortal class in the portal module . The class must be
imported from the location where the function is;
Now that the function's location has been defined, we may specify the
path to the class. Here, "portal" refers to the controller file, and
"CustomerPortal" to the function's class. Following these, we can
override the function by doing:
import CustomerPortal from
odoo.addons.portal.controllers.portal
Enterprise
If we need to modify, override, or alter the function, we can take over the
entire code, which resembles assuming complete control.
@http.route()
def _function(self, counters):
( #exisitingcode + added one)
return
However, when we use super(), the original controller function is
executed, and then we can extend it with our own custom logic:
@http.route()
def function(self, counters):
values = super()._function(counters)
#yourlogic
return
This is how the Web Controller is created, enhanced, and overridden in
Odoo 18.
For More Info.
Check our company website for related blogs
and Odoo book.
Check our YouTube channel for
functional and technical videos in Odoo.
Enterprise
www.cybrosys.com

How to Create & Configure Web Controllers in Odoo 18

  • 1.
    How to Create& Configure Web Controllers in Odoo 18 Enterprise
  • 2.
    Enterprise Introduction In this we’lldiscuss on how to create & configure web controllers in Odoo 18. In Odoo, the Front-end modules under "Website" are configured using "Controllers". These front-end modules and back- end modules are connected. We can define the URL to link the Web Pages using Controllers. In contrast to models, controllers offer their own extension method.
  • 3.
    Enterprise We must firstestablish a folder called "controllers" in our custom module in order to work with controllers. Init files and python files like models can be found in the Controllers folder. In contrast to models, controllers thus offer their own extension mechanism: Creating controllers involves deriving from the Controller class. Methods annotated with route() are used to define routes. We can create the controller using the sample code: from odoo import http from odoo.http import request class ControllerName(http.controller): @http.route(['/url/'], type="http", auth="public", website="True") def function_name(self, **post): Variable = request.env['models_name'].sudo().search([ ]) values = { 'records': Variable } return request.render("module_name.template_id", values)
  • 4.
    Enterprise To load thecontroller, we must first import the libraries, "http" and "request." Routes are defined through a method decorated with route(). Here we can see ‘@http.route()’ allows us to navigate to specific pages. And it also helps to link the URL with a specific webpage. Inside decorator, we can also use some keywords like; ● ‘/url’: Need to specify the ‘url’ to be used to redirect a specific page ● ‘type’: Specify the type of request. There are two types: a) ‘json’: This request is more suitable for executing methods on the server and obtaining results. b) ‘http’: This is simpler and easier to use when we want to access some resources on the server.
  • 5.
    Enterprise ● ‘auth’: It’defines the access to the URL or to view the webpage related to the link. a) ‘user’: If auth is the user, the current request will perform only for the user. b) ‘public’: If auth is public, the current request will perform for all the users. There will be no restrictions. c) ‘none’: If auth is none, nobody can access the request and database. ● ‘website’: We can mention that the controller is linking to the webpage by setting the value as ‘True’ or ‘False’.
  • 6.
    Enterprise ● ‘methods’: Thereare two methods. we can specify one, If not, all methods are allowed. a) [‘GET’]: GET is used for viewing something, without changing it, b) [‘POST’]: POST is used for changing something. ● ‘sitemap’: It Serves as the directory for website pages accessible to users. ● ‘multilang’: Used to add languages to your website and translate it from the frontend. ● ‘csrf’: It’s used for secure user sessions by generating a token.
  • 7.
    Enterprise Now we canrender the web page containing data, using This is for rendering the webpage.’request.render’ helps to render the webpage from the controller. The webpage template is defined by module_name.template_id. If you need to redirect to any another URL instead of rendering a web page, we use ‘request.redirect’. For example, if you need to redirect to ‘shop’, return request.render("module_name.template_id", values) return request.redirect('/shop')
  • 8.
    Enterprise Creating the View Wecan now construct the template, whose XML name should be "template_id" under the module's "views" subdirectory. since the view was previously invoked from the controller. <?xml version="1.0" encoding="utf-8"?> <odoo> <template id="template_id" name="Name"> <t t-call="website.layout"> <div class="oe_structure"> <div class="container"><br /> <center> <h3>Headline</h3> </center><br /> <table class="table-striped table"> <thead style="font-size: 23px;"> <tr> <h6><b> <th>Header1</th>
  • 9.
    Enterprise <th>Header 2</th> </b></h6> </tr> </thead> <tbody> <t t-foreach="records"t-as="order"> <tr> <td><span t- esc="order.value1" /></td> <td><span t- esc="order.value2" /></td> </tr> </t> </tbody> </table> </div> </div> </t> </template> </odoo>
  • 10.
    Enterprise We can alsooverride the current controller function in Odoo to suit our needs. It’s not the same as overriding in other python files. As an illustration, suppose we need to alter the controller function from the CustomerPortal class in the portal module . The class must be imported from the location where the function is; Now that the function's location has been defined, we may specify the path to the class. Here, "portal" refers to the controller file, and "CustomerPortal" to the function's class. Following these, we can override the function by doing: import CustomerPortal from odoo.addons.portal.controllers.portal
  • 11.
    Enterprise If we needto modify, override, or alter the function, we can take over the entire code, which resembles assuming complete control. @http.route() def _function(self, counters): ( #exisitingcode + added one) return However, when we use super(), the original controller function is executed, and then we can extend it with our own custom logic: @http.route() def function(self, counters): values = super()._function(counters) #yourlogic return This is how the Web Controller is created, enhanced, and overridden in Odoo 18.
  • 12.
    For More Info. Checkour company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com