I have recently upgraded to Django 1.2.5, and now I am having problems with localization, specifically number formatting. For example, in some templates I print the following samples:
data-id="{{ form.instance.id }}"
Which in cases >= 1000, used to evaluate to:
data-id="1235"
But now it actually results in (my localization is pt-BR, our decimal separator is dot):
data-id="1.235"
Which of course is not found when I afterwards query the database by ID. Using a |safe
filter solves the problem, but I'm not willing to find all IDs in all templates and safe them.
Usually, I'll only localize the floating points, not the integers. I don't want to disable L10N, because of all the other formatting that is working fine. Is there a way to make this distinction in Django localization? Any other solution is accepted.
Variable names consist of any combination of alphanumeric characters and the underscore ( "_" ) but may not start with an underscore, and may not be a number.
{% extends variable %} uses the value of variable . If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.
A for loop is used for iterating over a sequence, like looping over items in an array, a list, or a dictionary.
This flag tells Django that if a “safe” string is passed into your filter, the result will still be “safe” and if a non-safe string is passed in, Django will automatically escape it, if necessary. You can think of this as meaning “this filter is safe – it doesn't introduce any possibility of unsafe HTML.”
data-id="{{ form.instance.id|safe }}"
Also do the job
with django 1.2:
data-id="{{ form.instance.id|stringformat:'d' }}"
or, with django 1.3:
{% load l10n %}
{% localize off %}
data-id="{{ form.instance.id|stringformat:'d' }}"
{% endlocalize %}
or (also with django 1.3):
data-id="{{ form.instance.id|unlocalize }}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With