Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10927 - Content Types shortcut view throws 500s instead of 404s

Thanks to Jeremy Dunck/Alex Gaynor for the patch



git-svn-id: http://code.djangoproject.com/svn/django/trunk@11917 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 25ab93457c2c294420fa50302834f3623200b690 1 parent e4757ec
Luke Plant authored December 19, 2009
2  django/contrib/contenttypes/views.py
@@ -9,7 +9,7 @@ def shortcut(request, content_type_id, object_id):
9 9
     try:
10 10
         content_type = ContentType.objects.get(pk=content_type_id)
11 11
         obj = content_type.get_object_for_this_type(pk=object_id)
12  
-    except ObjectDoesNotExist:
  12
+    except (ObjectDoesNotExist, ValueError):
13 13
         raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id))
14 14
     try:
15 15
         absurl = obj.get_absolute_url()
28  tests/regressiontests/views/tests/defaults.py
@@ -10,8 +10,8 @@ class DefaultsTests(TestCase):
10 10
     """Test django views in django/views/defaults.py"""
11 11
     fixtures = ['testdata.json']
12 12
 
13  
-    def test_shorcut_with_absolute_url(self):
14  
-        "Can view a shortcut an Author object that has with a get_absolute_url method"
  13
+    def test_shortcut_with_absolute_url(self):
  14
+        "Can view a shortcut for an Author object that has a get_absolute_url method"
15 15
         for obj in Author.objects.all():
16 16
             short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, obj.pk)
17 17
             response = self.client.get(short_url)
@@ -19,12 +19,34 @@ def test_shorcut_with_absolute_url(self):
19 19
                                  status_code=302, target_status_code=404)
20 20
 
21 21
     def test_shortcut_no_absolute_url(self):
22  
-        "Shortcuts for an object that has with a get_absolute_url method raises 404"
  22
+        "Shortcuts for an object that has no get_absolute_url method raises 404"
23 23
         for obj in Article.objects.all():
24 24
             short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Article).id, obj.pk)
25 25
             response = self.client.get(short_url)
26 26
             self.assertEquals(response.status_code, 404)
27 27
 
  28
+    def test_wrong_type_pk(self):
  29
+        short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, 'nobody/expects')
  30
+        response = self.client.get(short_url)
  31
+        self.assertEquals(response.status_code, 404)
  32
+
  33
+    def test_shortcut_bad_pk(self):
  34
+        short_url = '/views/shortcut/%s/%s/' % (ContentType.objects.get_for_model(Author).id, '4242424242')
  35
+        response = self.client.get(short_url)
  36
+        self.assertEquals(response.status_code, 404)
  37
+
  38
+    def test_nonint_content_type(self):
  39
+        an_author = Author.objects.all()[0]
  40
+        short_url = '/views/shortcut/%s/%s/' % ('spam', an_author.pk)
  41
+        response = self.client.get(short_url)
  42
+        self.assertEquals(response.status_code, 404)
  43
+
  44
+    def test_bad_content_type(self):
  45
+        an_author = Author.objects.all()[0]
  46
+        short_url = '/views/shortcut/%s/%s/' % (4242424242, an_author.pk)
  47
+        response = self.client.get(short_url)
  48
+        self.assertEquals(response.status_code, 404)
  49
+
28 50
     def test_page_not_found(self):
29 51
         "A 404 status is returned by the page_not_found view"
30 52
         non_existing_urls = ['/views/non_existing_url/', # this is in urls.py

0 notes on commit 25ab934

Please sign in to comment.
Something went wrong with that request. Please try again.