How can i round a value in django template without using the filter - django-templates

Is there any way that I can do some python math function in django template. Actually I need to round a variable value and I want to achieve this without using the filters.
For example like {{math.round(total)}}.

You can use floatformat to round of the value in django template.
{{ total|floatformat }}
If you want to perform more mathematical operations, you can try django-mathfilters. Or you could write your custom template tag and perform the operations in that template tag.

#arulmr gave the best way to fixed it, In my case I also needed include
{{ total|floatformat:0 }}
In order to round the value with zero decimal places, and
{{ total|floatformat:1 }}
To define the decimal places

Related

using v-bind:href to bind an expression

I am trying to bind an expression to a URL using v-bind of vue.js.
The binder expression will be a year of the folder and I want that expression to render in the links.
I am trying to get this to bind correctly.
Can someone see what I am improperly concatenating?
<a v-bind:href="'https://www.exmaple.com'+{{$route.params.year | year}}'+'&FolderCTID=0x012000507B97BC3FFDCE4D854E'">My Link</a>
I think you're looking for this:
<a v-bind:href="'https://www.example.com/' + ($route.params.year || year) + '&FolderCTID=0x012000507B97BC3FFDCE4D854E'">My Link</a>
Key changes:
|| instead of |. The latter is a bitwise operator.
Added parentheses to ensure the || is applied correctly. Otherwise the + operators will be applied first.
Ditched the {{ ... }}. They shouldn't appear within v-bind expressions.
Removed the extra ' after the }}.
Added an extra / after the .com.
This will give, for example:
https://www.example.com/2019&FolderCTID=0x012000507B97BC3FFDCE4D854E
It seems likely you'd want a ? in there somewhere but that wasn't included in the original code.
You haven't mentioned what year is but it appears that it's a default for when the year route param is missing. That's certainly what I've assumed.
You may also want to consider using a computed property for this as it's a little complicated for template logic.
Update:
Based on the comment it seems that you intended | to be a Vue filter.
A filter can only be applied to the end of the whole expression. You cannot use it in the middle. If you try to use it in the middle it will just be interpreted as the JavaScript | operator and not as a filter.
I suggest using a method instead. If you have a method called formatYear it can be called using formatYear($route.params.year) within your v-bind expression.
I added this into my hyperlink and it works like a charm! I am sure someone will need this
+ $options.filters.year($route.params.year) +'

Vue.js interpolation values sum

There exist any way in Vue.js to make a sum between two interpolation values inside an html tag?
ex:
value1= 5
value2= 3
<span> {{value1}} + {{value2}}</span>
So I would like to know if its posible to obtain a third value rendered on the span tag adding the two values.
<span>{{value1 + value2}}</span>
I see no reason to manipulate data with operations on the template in this case.
The best (and simple) way, probably is to sum that values in your js code and render the result at template as another data property.
Although there is a Vue magic trick that allows you to made that.
It is called Computed Properties. Where you will be 'listening' some dependent data to create another data.
I totally recomend that you read Computed Properties documentation and see how it works
Here is the link:
https://v2.vuejs.org/v2/guide/computed.html
:)
Inside the {{}} you can run any JS really, so you should make a method that handles adding the 2 variables and inside that method it can check that both are numbers and handle converting strings if needed. Or you could try to turn this logic into a computed property.

Shopify liquid: Compare settings array with metafield value

I am trying to find a solution for the following problem. I have a list of lenses with different powers. They are starting from -12.00 and going up to +8.00. For the most part, they are 0.50 apart from the next one (6.00, 6.50, 7.00 s.o.).
I have a list with all the possible powers stored under settings, in an array. And then for each available brand of lenses it has a metafield with the minimum available power and the maximum one.
I envisioned it as a comparison, display only the powers from settings that are between these limits. However, I hit a limitation. I can't store 6.5 for example in a metafield as a number, it can only be a string and it makes it hard to compare the settings value with this one.
I tried to use a math filter (times), but the number is rounded. If I have "6.5" and I use times filter, I get "6".
Questions:
1. How would you do this?
2. How can I make sure that each value, if it is not a string, have two decimal places? (e.g. 6.50)
Let's say the metafield is like this product.metafields.power.value = 6.5. Do this:
{% assign power_value = product.metafields.power.value | plus: 0 %}
Boom. power_value is now a number.

Displaying dynamic kanban colors according to record state in OpenERP 7

could someone tell me in what way I can display the items in a view kanban with a specific color according to the state that is the record.
I'm trying something like this
<div t-attf-class="#{record.state=='scheduled' ? oe_kanban_color_#{kanban_getcolor(1)} : oe_kanban_color_#{kanban_getcolor(0)}">
but I looked ALL elements and not only those who are in the "scheduled".
Thanks :)
If you have copy/pasted exactly what you typed in the view definition, then your t-attf- class attribute is malformed, and all record will have the following class:
class="#{record.state=='scheduled' ? oe_kanban_color_1 : oe_kanban_color_0"
which, due to CSS class precedence, will cause them all to have the oe_kanban_color_1 style.
A few hints:
To avoid coloring some records, you can omit the oe_kanban_color_X entirely in some cases
You can use a t-att-class attribute to allow arbitrary Javascript expressions, depending on what you want to do. In contrast, t-attf-class only allows replacing placeholders.
When comparing field values with Javascript operators you normally want to use the value or raw_value of the field, rather that the Field object itself. value will only differ from raw_value when the value needs specific rendering, such as dates, numbers, etc.
The kanban_getcolor() function accepts any integer or string and returns one of the 10 default kanban color indexes.
Based on the above, the following might be closer to what you tried to do (note the t-att-class attribute:
<div t-att-class="record.state.value == 'scheduled' ?
'oe_kanban_color_1' :
'oe_kanban_color_0' ">
Alternatively, you could use t-attf-class and let kanban_getcolor() pick a color based on the state string:
<div t-attf-class="oe_kanban_color_#{kanban_getcolor(record.state.value)}">
That last example is similar to what is done in many default kanban views in the official OpenERP distribution.

String Template: is it possible to get the n-th element of a Java List in the template?

In String Template one can easily get an element of a Java Map within the template.
Is it possible to get the n-th element of an array in a similar way?
According to the String Template Cheat Sheet you can easily get the first or second element:
You can combine operations to say things like first(rest(names)) to get second element.
but it doesn't seem possible to get the n-th element easily. I usually transform my list into a map with list indexes as keys and do something like
map.("25")
Is there some easier/more straightforward way?
Sorry, there is no mechanism to get a[i].
There is no easy way getting n-th element of the list.
In my opinion this indicates that your view and business logic are not separated enough: knowledge of what magic number 25 means is spread in both tiers.
One possible solution might be converting list of values to object which provides meaning to the elements. For example, lets say list of String represents address lines, in which case instead of map.("3") you would write address.street.