Enable Dark Mode!
how-to-configure-web-controllers-in-odoo-16.jpg
By: Risvana AR

How to Configure Web Controllers in Odoo 16

Technical Odoo 16

In Odoo, the Front-end modules under "Website" are configured using "Controllers."These front-end modules and back-end modules are connected. You can define the URL to link the Webpages using Controllers. In contrast to models, controllers offer their own extension method.

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. It is similar to Models, controllers must support extensibility, but they are unable to do so using the same technique because the prerequisites (a database with loaded modules) may not yet be present (e.g., no database created or no database selected).
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.
Additionally, include this code in the module's init file to import the controller files from the folder.
from . import controllers
When we are creating the controller
from odoo import http
from odoo.http import request
class ControlerName(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)
To load the controller, we must first import the libraries. So I imported the libraries for "http" and "request."
@http.route(['/url/'], type="http", auth="public", website="True")
Routes are defined by a method with route decor (). Here, we can see that using "@http.route()" enables us to access particular pages. Additionally, connecting the URL to a certain website is helpful.
These keywords are used for interior decorators'
route - The pathways that are served by the decorated method are represented by route (Union[str, Iterable[str]). It will direct incoming HTTP request paths that match this route to this decorated method. The format of route expressions is described in the werkzeug routing documentation.
type (str) - The request type, which can be either "JSON" or "HTTP." It explains how to serialize the response and where to retrieve the request parameters.
auth - 
One of the following is used as the authentication method:
'user': The user must be verified before the current request may be carried out with the user's permissions.
'public' means that the user may or may not be verified. If not, the shared Public user will be used to carry out the current request.
"none": Even if there is no database, the technique is constantly in use. The framework and authentication modules are the main users of this. There will be no means for the request code to access the current user.
methods (verbs) that this route applies to are listed in methods (Iterable[str]). Any method is acceptable if none is provided.
There are two different techniques. If none are specified, any method may be used.
a) ['GET']: GET allows you to view something without altering it.
b) ['POST']: You change things using POST.
cors - The Access-Control-Allow-Origin cors directive value is represented by the string cors.
csrf (bool) - Indicates if CSRF protection for the route should be enabled. HTTP requests are enabled by default, but JSON requests are disabled by default.
sitemap  - This allows you to set sitemap links to demonstrate website navigation. It Serves as the directory for website pages accessible to users.
Multilang -  Used to add languages to your website and translate it from the front end.
Request: 
We can now render the data-filled web page. bring back values from the request.render("model_name.template_id").
This is used to render the website.
The controller's request.render' function aids in rendering the webpage. The module name and template id, in this case, "module_name" and "template_id," define the webpage template. Data can also be passed to it.
We use "request. redirect" if you want to route traffic to another URL rather than generating a webpage. Suppose you need to reroute to "shop," for instance. request for return.redirect('/redirect url')
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>
                                        <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>
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 module “CustomerPortal" from the module. “portal”
The class must be imported from the location where the function is;
Now that the function's location has been defined, you may specify the path to the class. Here, "portal" refers to the controller file, and "CustomerPortal" to the function's class. Following these, you can override the function by doing;
import CustomerPortal from odoo.addons.portal.controllers.portal
If you need to modify, override, or alter the function, you have taken the entire code. It resembles assuming complete control.
@http.route()
    def _function(self, counters):
        ( #exisitingcode + added one)
           
        return
The current function, as well as your customized function, must perform in order for you to Super the function.
@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 16.
The system aids in the development of front-end modules.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



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