0
votes

I need to set a JS variable value into a Odoo template variable value.

For example, the select_val value:

<script>
$(document).ready(function() {
 var button = $('#select_coas_report_sign');
 button.change(function() {
  select_val = $(this).val();
 });
});
</script>

I need to be setted into the next variable value:

<t t-set="my_var" t-value="select_val" />

Also, I need to set this variable into a function call in the template, but is giving me Server Error.

<t t-set="callUrl"
t-value="sale_order.get_portal_url(suffix='/accept_order/' + {{ my_var }})" />

enter image description here Any idea? Thanks for reading!

1
are you rendering the template in javascript with a context comming from javascript or are you rendering the template on the server side with a context comming from the server?bigbear3001
I think it's rendering by the server with a context comming from the server, any idea? @bigbear3001arevilla009

1 Answers

0
votes

You are mixing multiple types of rendering odoo templates into something that can not work.

Your javascript <script> tag is executed after the server renders the template so it can only set variables on the server side if you re render the orginal page. Usually that's done by creating a <form> and passing data via <input> to a controller. The controller then renders the template with the variable values from the inputs.

The error you are seeing TypeError: unhashable type: 'set' is because:

  • when doing <t t-set="callUrl" t-value="sale_order.get_portal_url(suffix='/accept_order/' + {{ my_var }})" /> the string inside t-value is run as a python expression
  • the python expression {{ my_var }} gives you the error TypeError: unhashable type: 'set'

To fix the error you could change setting the callUrl to:

<t t-set="callUrl" t-value="sale_order.get_portal_url(suffix='/accept_order/' + my_var )" /> Hint: the {{var}} is only used inside t-attf-$name

To prevent the error following from my_var being potentially None (TypeError: must be str, not NoneType) you could use python string formating:

<t t-set="callUrl" t-value="sale_order.get_portal_url(suffix='/accept_order/%s' % (my_var or '') )" />

As an alternative to posting a form, you can render templates in javascript if you do not need server side variables.