Skip to Content
Menu
This question has been flagged
2 Replies
3153 Views

Hi everybody,


I'm trying to assign the t-value of a t element (in an xml file) from a javascript function.


Here is the Javascript function: 

function get_country_code()
{

$.getJSON('https://api.db-ip.com/v2/free/self', function(data)
{
    all_data = JSON.stringify(data, null, 2);
    var country_code = all_data.split(',')[3].split(':')[1];
    console.log(country_code);
  });

}


While this is how I'm trying to assign my t-value, but I tried in different ways as well (changing the this keyword to window for example):

t t-set="country" t-value="this.get_country_code()"


But I always get this error: 

Error while render the template
KeyError: 'this'
Template: website.layout

What is weird is that, when I'm debugging in the console, by typing "this.get_country_code()" my function gets executed correctly.


If you need more details just let me know! Any kind of suggestion is accepted! Thanks!


Avatar
Discard
Best Answer

In Odoo, the this keyword only refers to the current object within the context of the Odoo environment. Therefore, you cannot use the this keyword to call a JavaScript function in your template. Instead, you can either call the function directly within your template using the t-esc attribute, or you can define a custom helper function in your template that calls your JavaScript function and returns the result.

Here is an example of how you can define a custom helper function in your template and call it using the t-esc attribute:

name="my_template">

Country code: t-esc="get_country_code()"/>

Avatar
Discard

Hi Ashish, i don' t well understand your answer, could you give us a more detailled code please? THX a lot

Author

Hi, I just want to update you guys on this, I've found a workaround to get the country code without using javascript.
.
.
The following function is used to retrieve the country code:

def get_country_id(self):
# request the data from the url
url = 'http://ipinfo.io/json'
request = requests.get(url)
json_data = request.json()

if not odoo_request:
# get the country
country_code = json_data['country']
else:
country_code = odoo_request.geoip.get('country_code')
country_id = self.env['res.country'].search([
('code', '=', country_code)], limit=1)

return country_id
.
.
The only downside of it is that you can only test this in staging since to work it needs to be outside of localhost (at least this is what I've found out when I made this code).
.
.
If you want to test it locally, this is the code I've been using:

def get_country_id(self):
# request the data from the url
url = 'http://ipinfo.io/json'
request = requests.get(url)
json_data = request.json()

# TODO: don't push these modifies. needed only for local testing
# if not odoo_request:
# get the country
country_code = json_data['country']
# else:
# country_code = odoo_request.geoip.get('country_code')
country_id = self.env['res.country'].search([
('code', '=', country_code)], limit=1)

return country_id
.
.
Hope somebody can find this helpful!

Author Best Answer

Thanks for the answer! I will try it out and let you know.

Avatar
Discard
Related Posts Replies Views Activity
1
Aug 20
7679
2
Jun 20
2893
2
Jul 24
286
2
Jun 23
40687
1
Dec 22
2317