Skip to Content
Odoo Menu
  • Prisijungti
  • Išbandykite nemokamai
  • Programėlės
    Finansai
    • Apskaita
    • Pateikimas apmokėjimui
    • Sąnaudos
    • Skaičiuoklė (BI)
    • Dokumentai
    • Pasirašymas
    Pardavimai
    • CRM
    • Pardavimai
    • Kasų sistema - Parduotuvė
    • Kasų sistema - Restoranas
    • Prenumeratos
    • Nuoma
    Svetainės
    • Svetainių kūrėjimo įrankis
    • El. Prekyba
    • Internetinis Tinklaraštis
    • Forumas
    • Tiesioginis pokalbis
    • eMokymasis
    Tiekimo grandinė
    • Atsarga
    • Gamyba
    • PLM
    • Įsigijimai
    • Priežiūra
    • Kokybė
    Žmogaus ištekliai
    • Darbuotojai
    • Įdarbinimas
    • Atostogos
    • Įvertinimai
    • Rekomendacijos
    • Transporto priemonės
    Rinkodara
    • Socialinė rinkodara
    • Rinkodara el. paštu
    • SMS rinkodara
    • Renginiai
    • Rinkodaros automatizavimas
    • Apklausos
    Paslaugos
    • Projektas
    • Darbo laiko žiniaraščiai
    • Priežiūros tarnyba
    • Pagalbos tarnyba
    • Planavimas
    • Rezervacijos
    Produktyvumas
    • Diskucija
    • Patvirtinimai
    • IoT
    • VoIP
    • Žinių biblioteka
    • WhatsApp
    Trečiųjų šalių programos Odoo Studija Odoo debesijos platforma
  • Pramonės šakos
    Mažmeninė prekyba
    • Knygynas
    • Drabužių parduotuvė
    • Baldų parduotuvė
    • Maisto prekių parduotuvė
    • Techninės įrangos parduotuvė
    • Žaislų parduotuvė
    Food & Hospitality
    • Barai ir pub'ai
    • Restoranas
    • Greitasis maistas
    • Guest House
    • Gėrimų platintojas
    • Hotel
    Nekilnojamasis turtas
    • Real Estate Agency
    • Architektūros įmonė
    • Konstrukcija
    • Estate Managament
    • Sodininkauti
    • Turto savininkų asociacija
    Konsultavimas
    • Accounting Firm
    • Odoo Partneris
    • Marketing Agency
    • Teisinė firma
    • Talentų paieška
    • Auditai & sertifikavimas
    Gamyba
    • Textile
    • Metal
    • Furnitures
    • Maistas
    • Brewery
    • Įmonių dovanos
    Sveikata & Fitnesas
    • Sporto klubas
    • Akinių parduotuvė
    • Fitneso Centras
    • Sveikatos praktikai
    • Vaistinė
    • Kirpėjas
    Trades
    • Handyman
    • IT įranga ir palaikymas
    • Saulės energijos sistemos
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Kiti
    • Nonprofit Organization
    • Aplinkos agentūra
    • Reklaminių stendų nuoma
    • Fotografavimas
    • Dviračių nuoma
    • Programinės įrangos perpardavėjas
    Browse all Industries
  • Bendrija
    Mokykitės
    • Mokomosios medžiagos
    • Dokumentacija
    • Sertifikatai
    • Mokymai
    • Internetinis Tinklaraštis
    • Tinklalaidės
    Skatinkite švietinimą
    • Švietimo programa
    • Scale Up! Verslo žaidimas
    • Aplankykite Odoo
    Gaukite programinę įrangą
    • Atsisiųsti
    • Palyginkite versijas
    • Leidimai
    Bendradarbiauti
    • Github
    • Forumas
    • Renginiai
    • Vertimai
    • Tapkite partneriu
    • Services for Partners
    • Registruokite jūsų apskaitos įmonę
    Gaukite paslaugas
    • Susiraskite partnerį
    • Susirask buhalterį
    • Susitikti su konsultantu
    • Diegimo paslaugos
    • Klientų rekomendavimas
    • Palaikymas
    • Atnaujinimai
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Gaukite demo
  • Kainodara
  • Pagalba

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Apskaita
  • Atsarga
  • PoS
  • Projektas
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Žymos (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Žymos (View all)
odoo accounting v14 pos v15
About this forum
Pagalba

Assign t-value in XML from JavaScript function

Prenumeruoti

Get notified when there's activity on this post

This question has been flagged
javascriptxmlt-value
2 Replies
5103 Rodiniai
Portretas
Alberto Mastini

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!


0
Portretas
Atmesti
Portretas
Ashish Hirpara
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()"/>

2
Portretas
Atmesti
GUILLAUME LEBOURG

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

Alberto Mastini
Autorius

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!

Portretas
Alberto Mastini
Autorius Best Answer

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

0
Portretas
Atmesti
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registracija
Related Posts Replies Rodiniai Veikla
Get data from JS file to XML file Solved
javascript xml
Portretas
Portretas
1
rugp. 20
9843
Comparing javascript varriable with t-if
javascript xml
Portretas
Portretas
Portretas
2
birž. 20
4963
How to hide "section-financials" from the ProductInfoPopup in POS Screen Solved
javascript xml odoo16
Portretas
Portretas
2
liep. 24
1977
how to call javascript function on button with xml in odoo? Solved
javascript xml odoo10
Portretas
Portretas
2
birž. 23
43366
How to relate js variable with XML field in (odoo 15)
javascript xml barcode
Portretas
Portretas
1
gruod. 22
4172
Bendrija
  • Mokomosios medžiagos
  • Dokumentacija
  • Forumas
Atvirasis kodas
  • Atsisiųsti
  • Github
  • Runbot
  • Vertimai
Paslaugos
  • Odoo.sh talpinimas
  • Palaikymas
  • Atnaujinti
  • Pritaikytas programavimo kūrimas
  • Švietimas
  • Susirask buhalterį
  • Susiraskite partnerį
  • Tapkite partneriu
Apie mus
  • Mūsų įmonė
  • Prekės ženklo turtas
  • Susisiekite su mumis
  • Darbo pasiūlymai
  • Renginiai
  • Tinklalaidės
  • Internetinis Tinklaraštis
  • Klientai
  • Teisinis • Privatumas
  • Saugumas
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo yra atvirojo kodo verslo programų rinkinys, kuris apima visas įmonės poreikius: CRM, El. Prekybą, Apskaitą, Atsargų, Kasų sistemą, Projektų valdymą ir kt.

Unikali Odoo vertės pasiūla – būti tuo pačiu metu labai lengvai naudojama ir visiškai integruota sistema.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now