﻿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
18381	"Underscore in CharField primary key breaks the admin ""view on site"" link"	dappawit	Florian Apolloner	"I have a model in which the primary key is a !CharField and the get_absolute_url method is defined. When I create an instance of the model with the primary key containing an underscore ""_"", the admin links for that object have '5F' (the ASCII hex code for an underscore) after the underscore.  For instance, if the primary key was 'abc_123', then the link to the object within the admin would contain 'abc_5F123'.  Strangely, these links work just fine.  And if you strip out the '5F', the links also work.  However, with the '5F' in the URL, the ''view on site'' link does ''not'' work.

To recreate the bug:

 1. I created a new project and enabled the Admin, both in settings.py and in urls.py.
 2. I created an app 'testapp' with the following models.py:

{{{#!python
from django.db import models

class MyModel(models.Model):
    id = models.CharField(max_length=100, primary_key=True)
    
    def get_absolute_url(self):
        return '/mymodel/{0}/'.format(self.id)
}}}

 3. I added 'testapp' to the INSTALLED_APPS setting.
 4. I started the dev server and created two instances of the '!MyModel' model in the admin: one with the primary key 'abc123' and one with the primary key 'abc_123'.
 5. Using the 'abc123' instance in the Admin site:
   a. it is at the Admin url of `/admin/testapp/mymodel/abc123/`, as expected.
   b. the ''view on site'' link points to `/admin/r/8/abc123/`, as expected.
   c. clicking on the ''view on site'' link tries to redirect me to `example.com/mymodel/abc123/` as expected.  (I'm actually taken to an IANA page about example.com being a fake domain.  But Chrome's developer tools shows that it first tried to redirect me to the example.com URL above.)
 6. Using the 'abc_123' instance in the Admin site:
   a. it is at the Admin url of `/admin/testapp/mymodel/abc_5F123/`
   b. the ''view on site'' link points to `/admin/r/8/abc_5F123/`, 
   c. clicking on the ''view on site'' link results in a 404 error at the `/admin/r/8/abc_5F123/` url.  I get the message: ""Content type 8 object abc_5F123 doesn't exist"".
   d. manually entering the url `/admin/r/8/abc_123/` (note: without the '5F') work as expected.
   e. manually entering the url `/admin/testapp/mymodel/abc_123/` does ''not'' work in this demo, although it worked in the full application where I first encountered the problem.

I'd like to be able to use the ''view on site'' link even when objects have underscores in their primary keys.  Even better would be to also remove the strange '5F' from the admin site URLs, although since they still work it is not absolutely necessary.

Tested with:
- Django 1.4 using SQLite
- both Chrome and Firefox
- Discovered the issue on Linux, then created small test app on Windows.

I hope that makes sense. Thanks."	Bug	closed	contrib.admin	1.4	Normal	fixed			Ready for checkin	0	0	0	0	0	0
