I am trying to format a string in a template to display as a currency. {{ object.cost }}
Would I be able to do something like "${:,.2f}".format({{ object.cost }}) in the template?
You can try to use floatformat filter:
{{ object.cost|floatformat:2 }}
where 2 means two decimals.
As, Django templates: output float with all decimal places
Related
I want to concatenate a string inside a template in Django (version 3). I have read everything I could find on the subject and found that this should be the way:
{{ "ST" | add: item.id | stringformat:"08d" }}
But it produces the error:
django.template.exceptions.TemplateSyntaxError: add requires 2 arguments, 1 provided
Can anybody shed light into my darkness?
As #Willem Van Onsum points out the id (pk) is an integer and has to be converted to a string before concatenating. Besides the spaces have to be removed for it to work. See: TemplateSyntaxError: 'with' expected with atleast one variable assignment
Hence the solution is:
{% with n=item.id|stringformat:"08d" %}
{{ "ST"|add:n }}
{% endwith %}
Also thanks to #jaswanth for his contribution.
I have a Decimal field in form.py like this:
`
quantity = forms.DecimalField(required=True, widget=forms.NumberInput(
attrs={'class': 'form-control item-quantity', 'style': 'text-align: right'}),
error_messages={'required': 'This field is required.'}, localize=True)
In html I have that:
`
{{ formset_item.management_form }}
{% for form in formset_item.forms %}
<tr class="gradeX">
<td>{{ form.quantity }}</td>
</tr>
{% endfor %}
Now I want to input number in this quantity input. ex: When I enter input (look like key press event) 1000000 into this input intext it will auto format to 1,000,000 for user see not insert to database
total=100000
print ("Total cost is: ${:,.2f}".format(total)
first of all you convert your o/p in string then after store in database try it....
lol.. sorry for my bad english :)
I'm not sure if I understand your requirements correctly, but I think I have the gist of it:
User can enter data in a form (numerical) which will be saved to the database as an integer
When the data is next presented to the user (for editing?) it will be formatted with commas (1,000 | 100,000 | 87,654,321) which then can be edited?
You may or may not want the system to convert entered numbers to comma-separated on the fly
For number 1 & 2, when are you pulling from and updating the database, you can simply convert the numbers to and from strings when needed with helper functions:
How to print number with commas as thousands separators?
How do I use Python to convert a string to a number if it has commas in it as thousands separators?
If you want it to happen in realtime, you will need Javascript. I would probably just have an onfocus listener that would convert the comma based number to a regular int form, then onblur it would convert it back to the comma formatted number (You would probably need some additional listeners to handle edge cases?).
Edit: If you want a more simple method, I am not familiar with it, but it seems this may be of help to you
Format numbers in django templates
In addition to above format localization comment, in the template you can formatting numbers as follows.
<!DOCTYPE html>
{% load humanize %}
{% for post in posts %}
Views : {{ post.views|intcomma }}
{% endfor %}
I have an array element called "tags" and would like to convert the array of tags to strings separated by a blank space. But how do you do that in Jinja?
I have tried:
{{ tags|join }}
Actually you are almost there, for join with space, just put it like this:
{{ tags|join(' ') }}
see the jinja docs for more details
You can use regular python in jinja tags. an obvious choice for some simple cases is str.join:
>>> jinja2.Template(r'{{ " ".join(bar) }}').render(bar='baz')
u'b a z'.
You can also iterate over sequences in jinja with a for block:
>>> jinja2.Template(r'{% for quux in bar %}{{ quux }} {% endfor %}').render(bar='baz')
u'b a z '
Hi all not sure how to explain this clearly but here goes....
I need to use two variables like so:
{% for client in clients %}
{% if user.client.username %}
I need {% if user.username %} but the value of username is in client.username from the loop.
is there a way to do this?
If I understand correctly, user is a dict, and you want to lookup the value indexed by client in each iteration of the loop - eg, user[client].username in Python.
This (deliberately) isn't possible in Django templates - the language is limited, to force you to do pre-processing in code.
Instead, you should zip your two lists/dicts together before passing them to the template.
Are you trying to do something if the value of client.username is equal to the value of user.client.username? If so, you want:
{% if client.username == user.client.username %} # Works in Django 1.2 and above
{% ifequal client.username user.client.username %} # Works everywhere
Is there an easy way to use python string formatting from within a django template? That is, I'd like to be able to do something like this in a template
{{ variable|%.3f }}
I know in this case, one can just use
{{ variable|floatformat:3 }}
But I'd really like to be able to generically use any python string format on a django variable. In my system it's inconvenient to have to deal with two different ways to format output (python vs django), so I'd like to standardize. I could write a custom template tag like
{% pyformat variable format="%.3f" %}
or maybe a custom template filter like
{{ variable|pyformat:"%.3f" }}
Do either of these already exist? Will the customer filter work with a string passed in like that?
{{ variable|stringformat:".3f" }}
Source: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#stringformat
stringformat
I had omit the "%":
{{ variable|stringformat:".3f" }}