Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
2 Antwoorden
3962 Weergaven

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
Annuleer
Beste antwoord

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
Annuleer

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

Auteur

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!

Auteur Beste antwoord

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

Avatar
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
1
aug. 20
8692
2
jun. 20
3715
2
jul. 24
936
2
jun. 23
41842
1
dec. 22
2992