Last active
August 29, 2015 14:13
-
-
Save soukiassianb/57eb911bcabac09f4871 to your computer and use it in GitHub Desktop.
Check if image field has the URL attribute associated with
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Products(models.Model): | |
# [....] | |
picture = models.ImageField(upload_to='products', blank=True, null=True) | |
picture_2 = models.ImageField(upload_to='products', blank=True, null=True) | |
picture_3 = models.ImageField(upload_to='products', blank=True, null=True) | |
# [....] | |
@property | |
def image_url_1(self): | |
if self.picture and hasattr(self.picture, 'url'): | |
return self.picture.url | |
@property | |
def image_url_2(self): | |
if self.picture_2 and hasattr(self.picture_2, 'url'): | |
return self.picture_2.url | |
@property | |
def image_url_3(self): | |
if self.picture_3 and hasattr(self.picture_3, 'url'): | |
return self.picture_3.url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Then in templates | |
{{ product.image_url_1|default_if_none:'#'}} | |
{{ product.image_url_2|default_if_none:'#'}} | |
{{ product.image_url_3|default_if_none:'#'}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by : Django The 'image' attribute has no file associated with it (stack overflow)