About Checking the Existence of a Linked File

Check the existence of the linked file using its storage attribute:

def get_logo_url(item):
    if item.logo and item.logo.storage.exists(item.logo.name):
        return item.logo.url
    return ""

This works with files saved in the file system, on AWS S3, or elsewhere.

Tips and Tricks Programming Django 5.x Django 4.2 Django 3.2 Amazon Web Services