How to prevent Django from finding Ids in the template?

Asked

Viewed 278 times

12

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

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

It is rendered as:

data-id="1.234"

Obviously if that ID goes to a request ajax, the ID is not found at the base because it is not a value int valid. 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 be necessary to change Django), for example:

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

It is possible to make Django not find Ids in a generalized way?

  • You can use custom templates for the admin without editing Django’s admin APP.

3 answers

13

2

You could use the

def __str__(self):
    return self.id

OR

def __unicode__(self):
    return self.id

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

{{ form.instance }}

About Admin, it is possible to create custom templates if necessary, but I imagine that it is not necessary, Django should assemble the forms and work properly even with internationalization enabled.

  • Then the {% url opts|admin_urlname:'changelist' %}{{ original.pk }} above be rendering the URL as <a href="admin/auth/user/25.263"> can be considered a bug?

  • What you are doing there is rendering the id in HTML, in a template of yours, where the location is enabled, if you are creating links to the admin manually in some template I imagine you need to use the |safe filter. I have never used links to Django admin urls using the tag url, but I imagine it should be possible to pass the parameters from within the tag not? something like {% url opts|admin_urlname:'changelist' original.id %}

  • No, my friend, this is Django code! But let it go, in the newer versions, this has been fixed.

0

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

Entree:

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

Exit:

data-id="1234"

Reference: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#safe

I just don’t understand why you are having problems with admin. At first it seems like you are customizing. It then says that the problem occurs as if it is not changing anything in the admin template.

  • The question already mentions the solution with |safe or |unlocalize. The problem happens everywhere ID is rendered on urls, and one of those places is admin (where I don’t want to overwrite just to fix it).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.