How to Configure Web Controllers in 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 should establish a folder called "controllers" in our custom module to work with controllers. Init files and python files like models can be found in the Controllers folder. It is identical to Models, controllers should 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 (i.e., no database created).

In contrast to models, controllers thus offer their own extension mechanism: Creating controllers involves deriving from the Controller class. Methods marked with the route () are used to outline routes.

Additionally, involve this code in the module's init file to import the controller files from the folder.

When we create the Odoo web controller.

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 permission.

'public': It 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.


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. In this case, the module name and template id "module name" and "template_id" define the webpage template. Additional 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.

We can override the current controller function in the controller in Odoo 16 to match our needs/requirements. 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" to the module. “portal”

The class must be essential 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;

The current function, as well as your customized function, must perform in order for you to Super the function.

This is how the Web Controller is developed, enhanced, and overridden in Odoo 16.

< Blogs>

More Blogs

Empowering Thoughts & Engaging Discussions

What is Knowledge Module in Odoo 16?


Why is Odoo an Ideal ERP for Food Manufacturers?

Odoo Partners

Why is Debugging Important in Odoo 15?