My model is returning a Decimal(1234567.50), I can't seem to display the Decimal with a thousands separator. Does Django have a way to do this? Do I need to create my own template filter?
Thanks.
You can use the intcomma filter.
Add humanize to the installed apps in settings.py and load it in the template.
{% load humanize %}
{{ my_num|intcomma }}
One easy way is to import locale and do the formatting in your view function.
Even easier to read this: http://docs.djangoproject.com/en/dev/topics/i18n/#overview
For format localization, it’s just
necessary to set USE_L10N = True in
your settings file. If USE_L10N is set
to True, Django will display numbers
and dates in the format of the current
locale. That includes field
representation on templates, and
allowed input formats on the admin.
You could add the following to settings.py:
USE_THOUSAND_SEPARATOR = True
That should work and you don't have to act with loading stuff.
https://docs.djangoproject.com/en/4.1/ref/settings/#use-thousand-separator
Related
python 2.7.5+. django_tables2 0.14.0
In settings.py:
USE_L10N = True
DATETIME_FORMAT = 'M j, Y H:i:s.u'
Didn't work. I tried {% load L10N %} at the top of my template. Didn't work.
I created a formats directory at the top of my project containing an en directory containing formats.py and added the following to my settings.py (yes, there are init.py files in the formats directory and in the en directory):
FORMAT_MODULE_PATH = '<project>.formats'
Didn't work.
I tried putting in {{created_at|date: 'M j, Y'}}{{created_at|time: 'H:i:s.u'}} before the {% render_table table %} tag from django_tables2. Got an error.
Tried the recommended django_tables2 method of, in my tables.py definition, adding to the Meta class:
localize('created_at')
Didn't work.
Tried putting 'localize=True' in the column definition for created_at in tables.py.
Didn't work.
The only thing that worked was modifying python2.7/site-packages/django/conf/locale/en/formats.py (in my virtual environment) but I won't be allowed to do that on my project because it's not considered portable.
I see stackoverflow entries that say this works but they are many years old and many django versions back. I see others here that say this doesn't work and they haven't/aren't going to fix it.
This functionality logs requests; during testing the other devs could really use seeing seconds and probably microseconds to help them debug but I can't seem to give it to them.
Any suggestions that I haven't already tried?
Thanks.
OK, blush - I got it. I made the following change in the model for the DateTimeField created_at:
#property
def nice_date(self):
# mm/dd/yyyy HH:mm:ss.uuuuuu
t = self.created_at.strftime("%m/%d/%Y %H:%M:%S.%f")
return t
Then, I displayed nice_date instead of created_at.
I hope this helps someone else.
I'm trying to set up the formatting of dates in templates based on a locale supplied by the user. As the rest of the page will remain in the original local ('en'), I only want my users'
supplied data to be formatted.
for example, dates.
users in the uk should be able to use l10n on the dates on their pages, but I don't want to set the whole site to be en_GB.
is there a way to set the locale in a template within a block, eg. something like
{% locale|'en_GB' %}
{{ my_date|localize }}
{% endlocale %}
You don't need to do anything explicit in the template.
Inside your settings.py define the FORMAT_MODULE_PATH setting.
Like:
FORMAT_MODULE_PATH = 'myproject.myapp.formats'
under the formats directory create one python package per supported language(other
than your default) of your project. Inside each of these you should have a formats.py
which should have any localized formatting options.
In my case the default language for my project is en, but I also support el(greek).
So I have this in my settings.py:
FORMAT_MODULE_PATH = 'myproject.websiteapp.formats'
Inside the myproject/websiteapp/formats directory I have a el package with a formats.py file, like:
el/
__init__.py
formats.py
Inside the formats.py I have this:
DATETIME_FORMAT="l j M Y, g:i a"
which is the greek specific representation of a date.
So when I use a datetime field inside my templates:
{{ mymodel.pub_date }}
It prints the default en representation when locale is set to the default:
Published on: Feb. 22, 2013, 1:47 p.m.
and my custom greek one when the locale is set to el.
Δημοσιεύτηκε: Τετάρτη 6 Φεβ 2013, 5:39 μμ.
More info here
Edit
Hmm, I just realized that you asked for specific template blocks or values.
Maybe the localize template filter or the localize template tag
are more relevant to your specific case?
I have a model that includes a TimeField object. I also have a django template that lists the time field in JSON Format
e.g.
...
"time":"{{ mymodel.mytime }}",
...
Without specifying any formatting/filters etc, this results in the following e.g.
....
"time":"5 p.m.",
....
However when I apply a time filter to my template i.e.
...
"time":"{{ mymodel.mytime|time:"H:i" }}",
...
I get the following result:
...
"time":"",
...
i.e. my time value is wiped out. I am not sure what's going on? I also have a datefield in the same model and the filter I am applying to that (in the same django template) which works as defined.
I am using Django 1.4.2
What am I missing or not doing correctly?
You have put time format in double quotes too.
try:
"time":"{{ mymodel.mytime|time:'H:i' }}",
or
"time":'{{ mymodel.mytime|time:"H:i" }}',
or
"time":"{{ mymodel.mytime|time:\"H:i\" }}",
time
Formats a time according to the given format.
Given format can be the predefined one TIME_FORMAT, or a custom format, same as the date filter. Note that the predefined format is locale-dependent.
For example:
{{ value|time:"H:i" }}
For some bizzare reason, not sure what.. It has started working! Must have been a typo or something in the filter format?! Really weird.
I am using Django's humanize to make large float numbers in my app more readable. Also my app is available in different languages, and different languages use , and . signs in numbers the other way around.
For integer values the humanize tag intcomma works well:
{{ intvalue|intcomma }}
Would give for different locales:
English: 1,000,000
Dutch: 1.000.000
However, using floats this doesn't work very well. For example in my template you would find this:
{{ floatvalue|floatformat:2|intcomma }}
Would give for different locales:
English: 1,000,000.00
Dutch: 1,000,000,00
Note that instead of 1.000.000,00 for Dutch, it shows 1,000,000,00. Switching around the floatformat:2 and intcomma tags doesn't work either as then the value is nothing.
Any ideas on how to easily fix this?
(If possible, I'd rather not use an external libraries such as Babel)
Try the Django Format Localization feature. It gives you three options. As the Django docs say,
Django’s formatting system is capable to display dates, times and numbers in templates using the format specified for the current locale. It also handles localized input in forms.
When it’s enabled, two users accessing the same content may see dates, times and numbers formatted in different ways, depending on the formats for their current locale.
The formatting system is disabled by default. To enable it, it’s necessary to set USE_L10N = True in your settings file.
Thus this first option turns on locale-dependent number formatting for all of your templates.
A second option is to use the localize template tag to turn on locale-dependent number formatting for only part of a template. For example,
{% load l10n %}
{% localize on %}
{{ value }}
{% endlocalize %}
A third option is to use the localize template filter to force locale-dependent number formatting for a single value. For example,
{% load l10n %}
{{ value|localize }}
I need to embed the current locale in a Django template's output (as part of a URL to be precise). I know that I can access the current language as {{ LANGUAGE_CODE }} if I { load i18n } but is there a similar way to access the current locale?
I suppose I could use to_locale() in the view logic and put it in the context for the template, but I'm looking for something more generic that might be part of the Django framework itself. Is there such a syntax?
I solved this by including code below in the template
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
and the variable LANGUAGE_CODE has the value you want (see also django docs for an example usage).
You might want to write your own context processor, which would call to_locale and automatically populate the context with the result -- it would just be something like this.
from django.utils.translation import to_locale, get_language
def locale(request):
return {'LOCALE': to_locale(get_language())}
I thought of implementing my own custom template tag that would simply output to_locale(get_language()) but the answer above is easier to implement so I like it better.