Chapter 9 - Odoo 15 Development Book

http route

When we create any customizations on the website, we should write the controllers. On controllers, there should be a decorator. The mainly used decorator is @http.route

‘@http.route’. It is a decorator that allows you to go to certain pages or for routing purposes. This also aids in linking the given URL to a certain web page. In http.route() type describes which kind of operation you want to perform.

Type = 'http' returns on another template, and it will take the dictionary with that. It will take you to another template with the values and will refresh the page.

Example:

@http.route('/demo_page', type="http")
Def demo_function(self):
//add button here
return "<h1>This is a test</h1>"

And type = 'json' calls from jsonrpc call from javascript and it will only return dictionary. It will not refresh the page and will do all the operations which is described in that method.

Example:

@http.route('/demo_page', type="json")
def demo_function(self):
//add button here
return {"sample_dictionary": "This is a sample JSON dictionary"}

Under route, we have to use some keywords.

  • ‘/url’ - Need to specify the ‘url’ that to use. Here, I have given the repair_webform, as needed.
  • ‘type’ - Specify the type of request. It can be ‘http’ or ‘json’ requests.
  • ‘auth’ - Defines who can access this URL or can view the webpage related to this link. It may,
  • ‘public’ - If auth=’public’, anyone can access the URL and view the webpage. There are no access restrictions.
  • ‘user’ - If auth=’user’, only logged-in users can access the URL and view the contents of the webpage.
  • ‘none’ - If auth=’none,’ it is always active and mainly used by the authentication modules.
  • ‘website=True’ - Here, we mention that this controller is linked to a webpage.

Serving static resources

Web pages contain several types of static resources, such as images, videos, CSS, and so on. We'll look at how to manage static resources for your module in this recipe.

Static files in Odoo are stored in the static/ subdirectory for each module. Therefore static files can be served by intercepting all requests to /MODULE/static/FILE and checking up the appropriate module (and file) in the various add-ons pathways.

Note: In general, a static HTTP server should serve all static files.

For example, On the page, we'll show an image. So, prepare one image.

First, create an img directory on your module (module_name/static/src/img) and In controller, create a new route. Replace the image URL with the URL of your image in the code:

@http.route('/website_demo', type='http', auth='none')
def demo(self):
image_url = '/module_name/static/src/image/img.png'
html_result = """<html>
<body>
<img src="%s"/>
</body>
</html>""" % image_url
return html_result

Then Restart the Odoo service and update the module to view the applied changes. Now, visit /website_demo to see the image on the page.

All files in the /static subdirectory are considered static resources and are accessible to the public. Our image is located in the /static/src/img directory in our example. The static resource can be placed anywhere in the static directory.

Also, we can add all static files like CSS, SCSS, XML, JS, etc.

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