{"record":{"id":"f4f755f8abee5984","repo":"django/django","slug":"content-type-ct-id-s-object-obj-id-s-doesn-t-e","errorCode":null,"errorMessage":"Content type %(ct_id)s object %(obj_id)s doesn’t exist","messagePattern":"Content type (.+?) object (.+?) doesn’t exist","errorType":"http","errorClass":"Http404","httpStatus":404,"severity":"warning","filePath":"django/contrib/contenttypes/views.py","lineNumber":23,"sourceCode":"from django.http import Http404, HttpResponseRedirect\nfrom django.utils.translation import gettext as _\n\n\ndef shortcut(request, content_type_id, object_id):\n    \"\"\"\n    Redirect to an object's page based on a content-type ID and an object ID.\n    \"\"\"\n    # Look up the object, making sure it's got a get_absolute_url() function.\n    try:\n        content_type = ContentType.objects.get(pk=content_type_id)\n        if not content_type.model_class():\n            raise Http404(\n                _(\"Content type %(ct_id)s object has no associated model\")\n                % {\"ct_id\": content_type_id}\n            )\n        obj = content_type.get_object_for_this_type(pk=object_id)\n    except (ObjectDoesNotExist, ValueError, ValidationError):\n        raise Http404(\n            _(\"Content type %(ct_id)s object %(obj_id)s doesn’t exist\")\n            % {\"ct_id\": content_type_id, \"obj_id\": object_id}\n        )\n\n    try:\n        get_absolute_url = obj.get_absolute_url\n    except AttributeError:\n        raise Http404(\n            _(\"%(ct_name)s objects don’t have a get_absolute_url() method\")\n            % {\"ct_name\": content_type.name}\n        )\n    absurl = get_absolute_url()\n\n    # Try to figure out the object's domain, so we can do a cross-site redirect\n    # if necessary.\n\n    # If the object actually defines a domain, we're done.\n    if absurl.startswith((\"http://\", \"https://\", \"//\")):","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/django/django/blob/ae25a40be07e8a749edf526df37c93e59d4a22c9/django/contrib/contenttypes/views.py#L5-L41","documentation":"Raised as Http404 by contenttypes shortcut() when looking up either the ContentType or the target object raises ObjectDoesNotExist, or when object_id cannot be parsed (ValueError), or fails validation (ValidationError). It signals that the (content_type_id, object_id) pair does not resolve to a real object.","triggerScenarios":"A request to /r/<ct_id>/<obj_id>/ where ct_id has no ContentType, obj_id has no matching object, obj_id is non-numeric (ValueError on int pk), or a custom pk field fails ValidationError.","commonSituations":"Stale links/breadcrumbs/bookmarks pointing at deleted objects; templates rendering links before objects exist; user-tampered URLs with arbitrary IDs.","solutions":["Verify the object exists before emitting the shortcut URL in templates (use get_absolute_url only on real instances).","Handle 404s in the frontend by re-fetching or hiding broken links.","Ensure object_id values are valid integer pks (or match the model's pk type) before link generation.","Run remove_stale_contenttypes if ContentType IDs are drifting."],"exampleFix":"// before (template emits link to maybe-deleted object):\n<a href=\"/r/{{ ct.id }}/{{ obj.id }}/\">view</a>\n\n// after (guard before rendering):\n{% if obj %}<a href=\"{{ obj.get_absolute_url }}\">view</a>{% endif %}","handlingStrategy":"validation","validationCode":"from django.contrib.contenttypes.models import ContentType\n\ndef resolve_or_none(ct_id, obj_id):\n    try:\n        ct = ContentType.objects.get(pk=ct_id)\n        if not ct.model_class():\n            return None\n        return ct.get_object_for_this_type(pk=obj_id)\n    except (ContentType.DoesNotExist, ValueError, Exception):\n        return None\n\nobj = resolve_or_none(content_type_id, object_id)\nif obj is None:\n    # render a graceful 'not found' instead of relying on Http404\n    ...","typeGuard":null,"tryCatchPattern":"from django.http import Http404\ntry:\n    obj = ct.get_object_for_this_type(pk=object_id)\nexcept (ObjectDoesNotExist, ValueError, ValidationError):\n    raise Http404('Resource not found')","preventionTips":["Validate object_id is the correct pk type before building shortcut URLs.","Only render contenttype shortcut links for objects you have confirmed exist.","Run remove_stale_contenttypes to keep ContentType IDs trustworthy."],"tags":["django","contenttypes","http404","object-lookup","broken-links"],"analyzedSha":"ae25a40be07e8a749edf526df37c93e59d4a22c9","analyzedAt":"2026-08-06T21:46:51.801Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}