check django object exists

from django.shortcuts import get_object_or_404
from .models import YourModel

def check_object_exists(request, object_id):
    # Step 1: Use get_object_or_404 to retrieve the object or raise a 404 error if not found
    obj = get_object_or_404(YourModel, id=object_id)

    # Step 2: Object exists, perform desired actions
    # Your code logic here...

    # No need to explicitly check for existence, as get_object_or_404 handles it
    # If the object is not found, the 404 response is raised and the subsequent code won't be executed
    return HttpResponse("Object exists, perform desired actions")