Home > Enterprise >  Django: Method to get url of images
Django: Method to get url of images

Time:07-17

Im working on an website currently i would like to switch from a class Product containing the images to following:

models.py:

class Product(models.Model):
    name    = models.CharField(max_length=60)
    price   = models.DecimalField(max_digits=10, decimal_places=2)
    
    # Digital Products do not need shipping
    digital = models.BooleanField(default=False, null=True, blank=False)
    # now being handled in ProductImage:
    # image   = models.ImageField(null=True, blank=True)

    @property
    def imageURL(self):
        try:
            url = self.image.url
        # get placeholder if something does not work
        except: 
            url = '/images/placeholder.png'
        return url
    

class ProductImage(models.Model):
    
    product     = models.ForeignKey(Product, related_name="images", on_delete=models.SET_DEFAULT, default=None, null=True)
    image1      = models.ImageField(null=True, blank=True)
    image2      = models.ImageField(null=True, blank=True)
    image3      = models.ImageField(null=True, blank=True)
    image4      = models.ImageField(null=True, blank=True)
    image5      = models.ImageField(null=True, blank=True)
    image6      = models.ImageField(null=True, blank=True)

i used to get the url of the single image (which works fine if image is an attribute of Product) as follows:

view.html:

{% for product in products %}    
    <img  src="{{ product.imageURL }}">
{% endfor %}

view.py:

def store_view(request):
    # some code
    "product":Product.objects.all()
    context.update({"products":products})
    return render(request, "view.html", context)

Is there a way of getting the images through the ForeignKey relation? (For example by modification of the above method imageURL)?

I have tried:

models.py:

class Product(models.Model)
    # same as above
    
    @property
    def imageURL(self):
        try:
            url = self.images.image1.url
        # get placeholder if something does not work
        except:
            url = '/images/placeholder.png'
        return url

but this raises an AttributeError: 'RelatedManager' object has no attribute 'image1'. Other possible solutions are appreciated as well.

CodePudding user response:

It is like that because self.images returns set of ProductImage objects. Even it is only one, you will have QuerySet object with one ProductImage object, like:

<QuerySet [<ProductImage object (1)>]>

You can change relation to OneToOneField if you don't plan to have more related ProductImage objects to Product. Then you can relate simply like you have tried:

class ProductImage(models.Model):
    
    product     = models.OneToOneField(Product, related_name="images", on_delete=models.SET_DEFAULT, default=None, null=True)
    image1      = models.ImageField(null=True, blank=True)
    ...

CodePudding user response:

in urls.py

urlpatterns = [

...

]   static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • Related