Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Django from localizing IDs in templates?

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.

like image 641
augustomen Avatar asked Apr 28 '11 18:04

augustomen


People also ask

Which characters are illegal in template variable names in Django?

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.

What does {% %} mean in Django?

{% 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.

What is Forloop counter in Django?

A for loop is used for iterating over a sequence, like looping over items in an array, a list, or a dictionary.

What does the built in Django template filter safe do?

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.”


2 Answers

data-id="{{ form.instance.id|safe }}"

Also do the job

like image 144
Richard Avatar answered Nov 26 '22 13:11

Richard


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 }}"
  • http://docs.djangoproject.com/en/1.3/topics/i18n/localization/#topic-l10n-templates
  • http://docs.djangoproject.com/en/1.3/ref/templates/builtins/#stringformat
like image 32
Mikhail Korobov Avatar answered Nov 26 '22 13:11

Mikhail Korobov