Opened 5 years ago

Closed 4 years ago

#17200 closed Bug (fixed)

"View on site" link breaks when quote(object_id) != object_id

Reported by: David Chandek-Stark Owned by: dlanger
Component: contrib.admin Version: master
Severity: Normal Keywords:
Cc: David Chandek-Stark, ldelaveau@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The change_view method of django.contrib.admin.options.ModelAdmin returns object_id as passed in. This means that the "View on site" link breaks in cases where the django.contrib.admin.util.quote(object_id) != object_id.

There are two possible solutions:
1) Call django.contrib.admin.util.unquote() on object_id before returning to change_view template;
2) Call django.contrib.admin.util.unquote() on object_id in django.contrib.contenttypes.views.shortcut in this line: obj = content_type.get_object_for_this_type(pk=object_id).

Attached are svn diffs for each.

Attachments (3)

django.contrib.admin.options.diff (571 bytes) - added by David Chandek-Stark 5 years ago.
django.contrib.contenttypes.views.diff (1.4 KB) - added by David Chandek-Stark 5 years ago.
17200-patch-and-test.diff (2.6 KB) - added by David Chandek-Stark 5 years ago.
Initial attempt at patch + unit test. Note that i18n not applied to "View on site" in test.

Download all attachments as: .zip

Change History (12)

Changed 5 years ago by David Chandek-Stark

Changed 5 years ago by David Chandek-Stark

comment:1 Changed 5 years ago by David Chandek-Stark

Cc: David Chandek-Stark added
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:2 Changed 5 years ago by Julien Phalip

Component: Uncategorizedcontrib.admin
Needs tests: set
Triage Stage: UnreviewedAccepted

Solution 1) is the right one. Could you provide a test case? Thanks!

comment:3 in reply to:  2 Changed 5 years ago by David Chandek-Stark

Replying to julien:

Solution 1) is the right one. Could you provide a test case? Thanks!

Happy to take a crack at a test case. Since I've not contributed one to Django before, I'm not sure the more helpful way to do this. My first guess is to write a test_viewonsite_link method in the AdminViewStringPrimaryKeyTest class of tests/regressiontests/admin_views/tests.py.

Changed 5 years ago by David Chandek-Stark

Attachment: 17200-patch-and-test.diff added

Initial attempt at patch + unit test. Note that i18n not applied to "View on site" in test.

comment:4 Changed 5 years ago by David Chandek-Stark

I realized that the new test could just be added to the test_get_change_view() method. As noted, i18n may need to be applied to "View on site" text in test.

comment:5 Changed 4 years ago by Claude Paroz

Needs tests: unset

comment:6 Changed 4 years ago by Claude Paroz

I guess that this issue might have been fixed along #18444. Can someone confirm?

comment:7 Changed 4 years ago by dlanger

Owner: changed from nobody to dlanger
Status: newassigned

comment:8 Changed 4 years ago by Ludovic Delaveau

Cc: ldelaveau@… added

I have the feeling that the test test_shortcut_view_with_escaping at line 1453 in tests/regressiontests/admin_views/tests.py confirms that the issue is fixed:

def test_shortcut_view_with_escaping(self):                                                              
    "'View on site should' work properly with char fields"                                               
    model = ModelWithStringPrimaryKey(pk='abc_123')                                                      
    model.save()                                                                                         
    response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(model.pk))                                                                                                        
    should_contain = '/%s/" class="viewsitelink">' % model.pk                                            
    self.assertContains(response, should_contain)                                                        

comment:9 Changed 4 years ago by Claude Paroz

Resolution: fixed
Status: assignedclosed

OK, let's consider it fixed.

Note: See TracTickets for help on using tickets.
Back to Top