[Fixed]-How to prevent Django from localizing IDs in templates?

12👍

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

Also do the job

9👍

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 }}"

0👍

This doesn’t really answer your question but check out this section of the docs. It says to use {{ |unlocalize }} filter or:

{% localize on %}
    {{ value }}
{% endlocalize %}

{% localize off %}
    {{ value }}
{% endlocalize %}

There’s probably a better way but I’m thinking that you could write a method that gives you the id as a string in your model for each model you are trying to display the id in a template.

class MyModel(models.Model):
    pass

    def str_id(self):
        return u'%s' % self.id

in your template:

{{ form.instance.str_id }}
👤dting

Leave a comment