[Django] #22226: Reversing admin URLs requires undocumented filter admin_urlquote.

17 views
Skip to first unread message

Django

unread,
Mar 7, 2014, 6:56:25 AM3/7/14
to django-...@googlegroups.com
#22226: Reversing admin URLs requires undocumented filter admin_urlquote.
-------------------------------+--------------------
Reporter: mattias | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
I have a model with a text primary key:
{{{
class CharKey(models.Model):
id = models.CharField(max_length=20, primary_key=True)
}}}

Reversing admin URLs as described in
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-
urls //sometimes// breaks depending on the characters in the key.

The admin pages call django.contrib.admin.utils.unquote() on the primary
key received in the URL. Internally, admin templates escape the object
URLs with for example
{{{
{% url opts|admin_urlname:'change' original.pk|admin_urlquote %}
}}}
The links obtained from the basic reverse(), as used in the documentation,
do not work:
{{{
reverse('admin:key_charkey_change', args=(c.id,))
}}}
as they fail to quote special charcters like the underscore "_". Instead,
the page linking to the admin has to use
django.contrib.admin.utils.quote() on the argument to reverse(), or the
corresponding template filter in a template.

The issue is surprisingly confusing because it seems that browsers like
Chrome auto-escape //most//, but not all special characters in a format
accepted and decoded by unquote(): for example, characters like $%!^&*()
work just fine without quote() in the code, but for example the underscore
does not. As a result the admin URLs without quoting work fine even with
funny characters in the primary key, unless one of the few exceptional
characters are used.

As a workaround, documenting the mandatory use of admin_urlquote template
filter and updating the examples might be enough.
However, I wonder if there are more general fixes to reverse() possible?
This seems like an issue that would come up in custom, non-admin URLs as
well.

I originally reported this in the comments of
https://code.djangoproject.com/ticket/18381, but created a separate issue
as requested. The behaviour of this ticket is present in the current
django master.

--
Ticket URL: <https://code.djangoproject.com/ticket/22226>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 20, 2014, 3:11:49 PM3/20/14
to django-...@googlegroups.com
#22226: Reversing admin URLs requires undocumented filter admin_urlquote.
-------------------------------+------------------------------------

Reporter: mattias | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by aaugustin):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/22226#comment:1>

Django

unread,
Mar 12, 2024, 2:19:59 AM3/12/24
to django-...@googlegroups.com
#22226: Reversing admin URLs requires undocumented filter admin_urlquote.
--------------------------------+------------------------------------
Reporter: Mattias Linnap | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by Ülgen Sarıkavak):

* cc: Ülgen Sarıkavak (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/22226#comment:2>
Reply all
Reply to author
Forward
0 new messages