Web Development
Http route
‘@http.route’ is a decorator which allows navigating to the URL specified in it.
@http.route('/contacts', type='http', auth='public', website=True)
Inside http.route(), we can define some parameters like;
1)/url: In this keyword, we need to specify the url that needs to be redirected.
In the above example, ‘/contacts’ is the url
2) type: The type keyword defines which kind of operation needs to be performed. type can have two values; ‘http’ or ‘json’. type=’http’ will return to the template, and it will take the dictionary with that.
type=’json’ will call from json rpc from javascript.
3) auth: auth describes who can access the specified URL. It can have values like;
a) auth=’public’: anyone can access the url, no restrictions.
b) auth=’user’: the logged-in user has only permission to access the url.
c) auth=’none’: it is always active and mainly used by the authentication modules.
4) website: this keyword describes whether this controller links to a web page or not. It can be True or False.
Serving static resources
A web page can have different types of static resources like images, videos, etc. Let’s look at how we can manage static resources in our custom modules. For this purpose, let us check how we can display an image on the web page. For that, follow the below listed steps;
1) Add your desired image in your custom module, inside the static directory. For example; /library_management/static/src/img/odoo16.png
2) Then define a controller.
@http.route(['/library'], type='http', auth="user", website=True)
def library_books(self):
partners =request.env['res.partner'].sudo().search([])
image_url = '/library_management/static/src/img/odoo16.png'
values = {}
values.update({'partners': partners, 'image_url': image_url})
return request.render("library_management.online_appointment_form", values)
In the above code, in place of image_url, you can add the url of your image.
3) Finally, from the template, you can fetch the image_url, and can be added like this;
<div class="form-group col-12 s_website_form">
<img t-att-src="image_url"/>
</div>
In this example, an image is displayed on the page. It’s also possible to display the image by directly from /library_management/static/src/img/odoo16.png
In Odoo, all the files that we will add inside the static directory are considered as static resources. In the above example, our image is placed inside the /static/src/img directory. Like wise, we can place different types of static resources inside the static directory.
For instance; the CSS files can be added inside the /static/src/css directory,
Javascript files in /static/src/js directory etc.