[Answer]-Accessing the first object in list to have an optional property in a Django template

1👍

maybe something like this… (with a custom filter!)

@register.filter
def first_with_attribute(some_list, attr):
     """        
     Usage in template:   {{ list|first_with_attribute:"cover_image" }}

     """
     for thing in some_list:
         if hasattr(some_list, attr):
              return getattr(some_list, attr)
     return ''    

Leave a comment