Přejít na obsah
Odoo Menu
  • Přihlásit se
  • Vyzkoušejte zdarma
  • Aplikace
    Finance
    • Účetnictví
    • Fakturace
    • Výdaje
    • Spreadsheet (BI)
    • Dokumenty
    • Podpisy
    Prodej
    • CRM
    • Prodej
    • POS Obchod
    • POS Restaurace
    • Předplatné
    • Pronájem
    Webové stránky
    • Webové stránky
    • E-shop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Dodavatelský řetězec
    • Sklad
    • Výroba
    • PLM
    • Nákup
    • Údržba
    • Kvalita
    Lidské zdroje
    • Zaměstnanci
    • Nábor
    • Volno
    • Hodnocení zaměstnanců
    • Doporučení
    • Vozový park
    Marketing
    • Marketing sociálních sítí
    • Emailový marketing
    • SMS Marketing
    • Události
    • Marketingová automatizace
    • Dotazníky
    Služby
    • Projekt
    • Časové výkazy
    • Práce v terénu
    • Helpdesk
    • Plánování
    • Schůzky
    Produktivita
    • Diskuze
    • Schvalování
    • IoT
    • VoIP
    • Znalosti
    • WhatsApp
    Aplikace třetích stran Odoo Studio Odoo cloudová platforma
  • Branže
    Maloobchod
    • Knihkupectví
    • Obchod s oblečením
    • Obchod s nábytkem
    • Potraviny
    • Obchod s hardwarem
    • Hračkářství
    Jídlo a pohostinství
    • Bar a Pub
    • Restaurace
    • Fast Food
    • Penzion
    • Distributor nápojů
    • Hotel
    Nemovitost
    • Realitní kancelář
    • Architektonická firma
    • Stavba
    • Správa nemovitostí
    • Zahradnictví
    • Asociace vlastníků nemovitosti
    Poradenství
    • Účetní firma
    • Odoo Partner
    • Marketingová agentura
    • Právník
    • Akvizice talentů
    • Audit a certifikace
    Výroba
    • Textil
    • Kov
    • Nábytek
    • Jídlo
    • Pivovar
    • Korporátní dárky
    Zdraví a fitness
    • Sportovní klub
    • Prodejna brýli
    • Fitness Centrum
    • Wellness praktikové
    • Lékárna
    • Kadeřnictví
    Transakce
    • Údržbář
    • Podpora IT & hardware
    • Systémy solární energie
    • Výrobce obuvi
    • Úklidové služby
    • Služby HVAC
    Ostatní
    • Nezisková organizace
    • Agentura pro životní prostředí
    • Pronájem billboardů
    • Fotografování
    • Leasing jízdních kol
    • Prodejce softwaru
    Procházet všechna odvětví
  • Komunita
    Edukační program
    • Tutoriály
    • Dokumentace
    • Certifikace
    • Vzdělávání
    • Blog
    • Podcast
    Podpora vzdělávání
    • Vzdělávací program
    • Scale Up! Hra na firmu
    • Navštivte Odoo
    Získat software
    • Stáhnout
    • Porovnejte edice
    • Verze
    Spolupráce
    • Github
    • Fórum
    • Události
    • Překlady
    • Stát se partnerem
    • Služby pro partnery
    • Registrujte svou účetní firmu
    Získat služby
    • Najít partnera
    • Najít účetní
    • Setkejte se s poradcem
    • Implementační služby
    • Zákaznické reference
    • Podpora
    • Upgrady
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dohodnout demo
  • Ceník
  • Pomoc

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

  • CRM
  • e-Commerce
  • Účetnictví
  • Sklad
  • PoS
  • Projekty
  • MRP
All apps
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
Pomoc

Assign t-value in XML from JavaScript function

Odebírat

Get notified when there's activity on this post

This question has been flagged
javascriptxmlt-value
2 Odpovědi
5120 Zobrazení
Avatar
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
Avatar
Zrušit
Avatar
Ashish Hirpara
Nejlepší odpověď

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
Avatar
Zrušit
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
Autor

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!

Avatar
Alberto Mastini
Autor Nejlepší odpověď

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

0
Avatar
Zrušit
Enjoying the discussion? Don't just read, join in!

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

Přihlásit se
Related Posts Odpovědi Zobrazení Aktivita
Get data from JS file to XML file Vyřešeno
javascript xml
Avatar
Avatar
1
srp 20
9866
Comparing javascript varriable with t-if
javascript xml
Avatar
Avatar
Avatar
2
čvn 20
4983
How to hide "section-financials" from the ProductInfoPopup in POS Screen Vyřešeno
javascript xml odoo16
Avatar
Avatar
2
čvc 24
1984
how to call javascript function on button with xml in odoo? Vyřešeno
javascript xml odoo10
Avatar
Avatar
2
čvn 23
43380
How to relate js variable with XML field in (odoo 15)
javascript xml barcode
Avatar
Avatar
1
pro 22
4180
Komunita
  • Tutoriály
  • Dokumentace
  • Fórum
Open Source
  • Stáhnout
  • Github
  • Runbot
  • Překlady
Služby
  • Odoo.sh hostování
  • Podpora
  • Upgrade
  • Nestandardní vývoj
  • Edukační program
  • Najít účetní
  • Najít partnera
  • Stát se partnerem
O nás
  • Naše společnost
  • Podklady značky
  • Kontakujte nás
  • Práce
  • Události
  • Podcast
  • Blog
  • Zákazníci
  • Právní dokumenty • Soukromí
  • Zabezpečení
الْعَرَبيّة 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 je balíček open-source aplikací, které pokrývají všechny potřeby vaší společnosti: CRM, e-shop, účetnictví, sklady, kasy, projektové řízení a další.

Unikátní nabídka od Odoo poskytuje velmi jednoduché uživatelské rozhraní a vše je integrované na jednom místě.

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