How do I prevent Django from finding IDs in the template?

10

I'm using Django 1.4 with location (L10N) enabled, which causes numeric values in the template to be formatted: 1.234,56 . The problem is that every time I put ID in the template, for example:

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

It is rendered as:

data-id="1.234"

Obviously, if this ID goes to request ajax , the ID is not found in the base, because it is not a valid int value. I I can avoid this behavior using |safe or |unlocalize , but in some places, for example in admin , I do not have this access (would need to change Django) / p>

<a href="{% url opts|admin_urlname:'changelist' %}{{ original.pk }}">{{ original|truncatewords:"18" }}</a>

Is it possible to make Django not find IDs generically?

    
asked by anonymous 11.12.2013 / 17:21

3 answers

12

You can disable the find on just one value:

{% load l10n %}
{{ value|unlocalize }}

Or you can disable in some part of the template:

{% load l10n %}
{% localize off %}
    {{ value }}
{% endlocalize %}

Source: link

    
11.12.2013 / 22:38
1

You could use

def __str__(self):
    return self.id

OR

def __unicode__(self):
    return self.id

And use the class itself as a string. It is not the ideal way, but it would work.

{{ form.instance }}

On Admin, you can create custom templates if necessary, but I imagine it is not necessary, Django should assemble the forms and work correctly even with internationalization enabled.

    
11.12.2013 / 18:27
0

You can avoid this numeric formatting using the template tag safe :

Entry:

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

Output:

data-id="1234"

Reference: link

I just do not understand why you're having a problem with admin. At first it looks like you're customizing, then says that the problem occurs as if you were not changing anything in the admin template.

    
23.05.2015 / 00:19