Ticket #10223: 10223-charfield_pk_with_slashes_view_on_site-1.0.x.diff

File 10223-charfield_pk_with_slashes_view_on_site-1.0.x.diff, 2.2 KB (added by Ramiro Morales, 15 years ago)

Patch from HuCy and tests from mmarshall adapted for the 1.0.x branch as of r10618

  • django/contrib/admin/sites.py

    diff -r 6a15d764cd1d django/contrib/admin/sites.py
    a b  
    152152        # URLs starting with 'r/' are for the "View on site" links.
    153153        elif url.startswith('r/'):
    154154            from django.contrib.contenttypes.views import shortcut
    155             return shortcut(request, *url.split('/')[1:])
     155            return shortcut(request, *url.split('/', 2)[1:])
    156156        else:
    157157            if '/' in url:
    158158                return self.model_page(request, *url.split('/', 2))
  • tests/regressiontests/admin_views/models.py

    diff -r 6a15d764cd1d tests/regressiontests/admin_views/models.py
    a b  
    121121
    122122    def __unicode__(self):
    123123        return self.id
     124
     125    def get_absolute_url(self):
     126        return "/" + self.id
    124127
    125128class Color(models.Model):
    126129    value = models.CharField(max_length=10)
  • tests/regressiontests/admin_views/tests.py

    diff -r 6a15d764cd1d tests/regressiontests/admin_views/tests.py
    a b  
    227227            '<a href="?surface__exact=y">Vertical</a>' in response.content,
    228228            "Changelist filter isn't showing options contained inside a model field 'choices' option named group."
    229229        )
     230
     231    def testShortcutWithSlash(self):
     232        """Ensures the 'view on Site' links work for models with string PKs values that contain slashes."""
     233        obj = ModelWithStringPrimaryKey(id="pk/with/slashes")
     234        obj.save()
     235        content_type_pk = ContentType.objects.get_for_model(ModelWithStringPrimaryKey).pk
     236        short_url = '/test_admin/%s/r/%s/%s/' % (self.urlbit, content_type_pk, obj.pk)
     237        response = self.client.get(short_url)
     238        self.assertRedirects(response, 'http://example.com%s' % obj.get_absolute_url(),
     239                             status_code=302, target_status_code=404)
     240
    230241
    231242def get_perm(Model, perm):
    232243    """Return the permission object, for the Model"""
Back to Top