jinja format strings that could be "None" - jinja2

I'm getting started with Jinja by converting my Django templates. Let's say I have a variable that represents a dollar value. So if I want to format it to two decimal places, I would do this:
{{"%.2f" | format(my_dollar_var)}}
But what if my_dollar_var is None? In that case, I'd like to show something else (like a question mark or a dash -- but not a zero).

I use a customer Currency filter:
def Currency(value):
if(value == None):
return "???"
else:
return "${:,.2f}".format(float(value))
jinja2.filters.FILTERS['Currency'] = Currency
Then just use:
{{ PRICE | Currency }}
Hope this helps!

Related

How to remove 1000 factor commas from currency pipe in angular

Is there a way to remove the commas when using Currency pipe?
<div>{{balance | currency }}</div>
Output like this - $7,885,412.00
I want like this - $7885412.00
You can use Angular Inbuilt replace pipe.
<div> {{balance | currency | replace:',':''}}</div>
So, i just made a test app that worked.
generate a pipe using cli, as that will register it for you too with:
ng g p noComma
inside the no-comma.pipe.ts
replace the transform section with:
transform(value: number): string {
if (value !== undefined && value !== null) {
return value.toString().replace(/,/g, '');
} else {
return '';
}
}
then in your view,
{{ balance | currency | noComma }}

How to convert string to double in angular

I am using a payments API in my application and am getting the invoice value with type number
signature.invoice.amount: 10500 (Number)
I tried to use currency pipe {{ signature.invoice.amount | currency: 'BRL': true}} and the transformed value is: $ 10,500.00.
But the transformed value should be $ 105.00 ... how do I get the number of the amount received to a monetary value in 'BRL' with a return of the number type?
Since $105.00 is 10,500 cents, you want to convert it to dollars before transforming it:
{{ signature.invoice.amount / 100 | currency: 'BRL': true}}
Good job storing currency as cents though! That's the way to do it to save yourself errors down the line.

How to change to string and remove ' from Django tempalates / context?

I currently have a date that is formatted in unicode:
k = u'2015-02-01'
I tried to add this to a list and change it into a string:
date = []
date.append(str(k))
Then I want to pass this as a Django context to my template.
However, the date is showing up with the following:
'2015-02-01'
How do I just rid of $#39; and replace it with a double quote (")?
Thanks much.
You can try to prevent string escape in template like this:
{{ variable|safe }}
In-view way:
from django.utils.safestring import mark_safe
from django.template import Context
data=mark_safe(data)
inescapable = Context({'data': data}, autoescape=False)
I know this is old but other people might stumble upon this with the same problem
Try
{% autoscape off %} {{ date }} {% endautoscape %}
It worked fine for me
When requesting graphs from google charts the data must be sent as a text array. The csv file has to be pure text with no apostrophes.
however
code fragment
data = repr(textData)
returns data bounded by ' '
this is interpreted as "&#39" in html
The solution to this is to javascript split method
var par = textData.split(""&#39") textArray = par[1] // the part without '
rest of code

String concatenation of key in map with variable in angularjs

So I have this angularjs code in my html where I am trying to access the value of a key/value pair like so:
<td>{{ mapA.[stringX. + mapB.keyC + .stringY]}}</td>
The value of mapB.keyC should concatenated to the String key to get the value of a particular key/value pair, so that the name of the key for example would be something like stringX.valueC.stringY to return the value of the map
How would I do this in angularjs and/or in pure javascript?
EDIT:
I tried to be general, however in my case I have a credit meter with a particular local currency as within its name e.g. "credit.in.cash.GBP.2000" and I want to get the value of it with:
{{ meter.[credit.in.cash. + localCurrency.ISO_CODE + .2000] / 100 | currency : localCurrency.SYMBOL : 2 }}
expected e.g. £2000

Jira JSON date format

I am using the Jira API, and need the start and end dates for a sprint.
The JSON data I get back is :
{"jodaTimeZoneId":"Europe/Berlin","sprints":[{"id":5,"start":"13082015044305","end":"27082015044305",...
Normally, json returns the date in milliseconds, and you need to deserialize that.
Now however, I can clearly see the date (13-08-2015 & 27-08-2015) followed by some other numbers I don't care about. Is there anyway Angular can get the correct format using | date? Or any other way I can use?
When I use {{13082015044305 | date:'dd-MM-yyyy'}} it returns 21-07-2384. The parsing date format is wrong. So change the format to recognized way.
So I used
input.toString().replace(/(\d\d)(\d\d)(\d\d\d\d)(\d\d\d\d\d\d)/, '$1-$2-$3');
Used it in a custom filter.
app.filter('correctDateFormat', function() {
return function(input) {
return input.toString().replace(/(\d\d)(\d\d)(\d\d\d\d)(\d\d\d\d\d\d)/, '$1-$2-$3');
};
});
Then
Display the date as
{{13082015044305 | correctDateFormat }}
I think you can use
{{ data | filter:options }}
where data is your json and date filter
{{'1388123412323' | date:'MM/dd/yyyy # h:mma'}}
an option like this.