""
Home Blog How to Change Home Action in Odoo Through Python
How to Change Home Action in Odoo Through Python

How to Change Home Action in Odoo Through Python

In today’s highly competitive business environment, increased customer engagement is visible in almost all business process. The inputs from customer at every stage will ensure the quality enhancement of the products. In this blog, we’ll learn how to change home action for an External User through Python in Odoo.

In Odoo, customer can provides these inputs by logging in as a portal user in Odoo. With this customer portal service, customers can access & interact with the organization providing necessary inputs timely.

There are three types of user in Odoo, Internal User, public user and portal(or external) user. An internal user can be the administrator which is the default user created at first and has complete access to the system. 

Then, this user can create as many as users and assign their permissions and access rights within the application. In this way, admin can create organizational hierarchy and restrict users only to their own domain.

The customers have an option to access the allowed area of the Odoo as an external user. An external user is a normal user who is, in fact, external to the system, he can sign up and create his profile and access the system. For using this facility, it must be enabled in Settings.


 Subscribe for Odoo tips, technical insights, and more!


The Odoo system also provides the feature to set a default or custom home page for every type of user. Or we can say set a homepage for the User, whenever that User log-in to the system.

By default in Odoo 12, the homepage for internal user is ‘/web’ and for external user is ‘/my’. 

Now what if we want to change the custom homepage for external user.


By default, the custom homepage for an internal user is this:

By default, the custom homepage for an internal user

That is, it navigates to :  ‘/web’

And for an external user, the custom homepage is like this:

For an external user, the custom homepage

That is, it navigates to : ‘/my’.

The coding (or logic) for this re-direction is done here:

‘addons/website/controllers/main.py’

Steps to follow:  Change Home Action in Odoo Through Python

 So this method is responsible for navigating to the custom homepages for respective users.

Now what if we want to change the custom homepage for the external user from ‘/my/’ to ‘/dashboard’.

So, how can we do so?? We can do this by overriding this method of website and changing the redirection homepage for the external user.

So in this case, we’ll do this through a custom module. First of all, we’ll create a folder named ‘controllers’. In this folder, we’ll create an ‘__init__.py’ file. Now what we’ll do is create a new .py file named ‘main.py’. Following the basic syntax of a module in Odoo, we’ll import the ‘main.py’ file inside the ‘__init__.py’ file. Now comes the most important part that is overriding the method of the website module.

You can do this like this:

Steps to follow:  Change Home Action in Odoo Through Python

In the above code,

from odoo import http

from odoo.http import request

from odoo.addons.website.controllers.main import Website

 

****** In the above code,packages are imported**********

Most important part is to import the ‘Website’ method from the ‘main.py’ file from the controllers of the website module.

Now we’ll override this ‘Website’ method by calling like this:

class Website(Website):

    @http.route(website=True, auth="public")

    def web_login(self, redirect=None, *args, **kw):

        response = super(Website, self).web_login(redirect=redirect, *args, **kw)

        if not redirect and request.params['login_success']:

            if request.env['res.users'].browse(request.uid).has_group('base.group_user'):

                redirect = b'/web?' + request.httprequest.query_string

            else:

                redirect = '/dashboard'

            return http.redirect_with_hash(redirect)

        return response

So the only change that we have made is changing the code from :

          redirect = ‘/my’

to

          redirect = ‘/dashboard’

Now, we’ll just save and update the custom module. After that we’ll check by logging in as an external user.
 

The custom homepage for the external user will be redirected like this:

custom homepage for the external user

So, as you can see by overriding the home action of the controllers through python code, we can change the custom homepage for the Users.

If you have any requirements for Odoo development services, You can also mail us at [email protected]

Get in touch wit​h us!

Protected by reCAPTCHA, Privacy Policy & Terms of Service apply.
Leave a Comment

Your email address will not be published.

Submit
Your comment is under review by our moderation team.