﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22266	CharField primary keys with underscores are (un)escaped differently in the admin pages	victor.varvariuc@…	nott	"Migrated from here: https://code.djangoproject.com/ticket/18381#comment:6

Although that bug was fixed 2 years ago, related issues seem still present in the latest stable Django release 1.6.2.
CharField primary keys with underscores are (un)escaped differently in the admin pages, and when reversing admin URLs with django.core.urlresolvers.reverse() or the {% url %} template tag.
The admin pages add ""5F"" after the underscore in when rendering URLs, but reverse() does not. As a result, links from custom pages into the admin break when the object's primary key contains underscores.

Steps to reproduce:
- Create a model with ""id = models.CharField(max_length=100, primary_key=True)"" or other similar text primary key field,
- Register the model to the admin site,
- Create a model instance with test_123 as the primary key,
- Visit the admin, and observe admin URLs: they are of the format ""/admin/<app>/<model>/test_5F123/""
- Open the shell, and call reverse('admin:app_model_delete', args=['test_123'])
- The URLs returned are of the format ""/admin/<app>/<model>/test_123/"" - no 5F
- As a result, if these links are embedded in pages, they don't work - the admin page tries to decode the primary key, expects 5F, and does not find the object.

{{{
[trains] vic@vic ~/projects/trains (master *) $ ./manage.py shell
Python 3.3.2+ (default, Oct  9 2013, 14:50:09) 

>>> from django.core.urlresolvers import reverse

>>> reverse('admin:trains_route_change', args=('test_123',))
>>> '/trains/route/test_123/'
}}}

"	Bug	closed	contrib.admin	1.6	Normal	fixed			Accepted	0	0	0	0	0	0
