Aide

2

How to get current url in odoo 9?

Avatar
Prince Caspion

Hello all,

I want to fetch the current url like  this :http://localhost:8069/web?debug=#id=10&view_type=form&model=crm.lead&action=251

in method.

How can we do that? i've tried with several ways but didn't work for me. I can get the base url using config parameter but not ful URL.

Regards,

Prince.


 


Avatar
Discard
2 Réponses
2
Avatar
deep
Best Answer

You can get the current url from Prams object as follows

base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')


or you can fetch the details from self.environment



2 Comments
Avatar
Discard
Avatar
Prince Caspion
-

I've already tried this one but it just gives base url not complete url. i need complete url.

Avatar
deep
-

Then fetch the details from self.environment or http/request

1
Best Answer

Hi Prince, You try my way


Step1 : Create a javascript file and call a python function when URL changes

refer : https://www.odoo.com/forum/help-1/question/odoo-9-website-js-help-109603#answer-109604

Pass the current url in that function

Example

$(window).on('hashchange', function(e){
    ajax.jsonRpc("/action_set_current_url/", 'call', window.location.href)
});


Step2: In python function, Set the current url to session.

Example

from openerp import http
from openerp.http import request
class YourModel(http.Controller):
    @http.route(['/action_set_current_url/<string:url>'], type='json', auth="public")
    def set_current_url(self, url,**post):
        # CODE TO SET COOKIES
        request.session['my_current_url'] = url


Step3:

 Call the current url from your function

 Example

def your_function(self):
    # CODE TO GET URL
    current_url = request.session.get('my_current_url')


Note : my code is not tested

4 Comments
Avatar
Discard
Avatar
Prince Caspion
-

thanks for your answer and quick response. but the problem is my function is called in back end it is not dealing with any web/js action. I am not sure, can i get the current full path in the method which is called in button click.

Avatar
Shameem Babu @ iWesabe
-

I have tried many times

https://www.odoo.com/forum/help-1/question/how-to-get-current-url-from-bowser-101333#answer-108714

I found this is only one solution

It is possible in backend

Link JS like this

<template id="assets_backend" name="im assets" inherit_id="web.assets_backend">

<xpath expr="." position="inside">

<script type="text/javascript" src="/path /to/file.js"></script>

</xpath>

</template>

Avatar
Prince Caspion
-

thanks, i will check it.

Avatar
Shameem Babu @ iWesabe
-

A small change in my answer. I told to write script when document is ready.

But you need to write when url changes