I have created a model called Product that takes in an ImageField that will be upload on a function I created:
class Product(models.Model):
title = models.CharField(max_length=120)
description = models.TextField()
price = models.DecimalField(decimal_places=2, max_digits=10, default=39.99)
image = models.ImageField(upload_to=upload_image_path, null= True, blank=True)
def __str__(self):
return self.title
return self.image
def __unicode__(self):
return self.title
I have also created my MEDIA_ROOT and STATIC_ROOT below is code from main urls however I also defined this two on the settings.py:
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
So I am able to upload Image from the admin and it uploads properly to the media_root that I created.
When I try calling/displaying the image it render nothing I do not know where I could have made a mistake
{% load static %}
{{ object.title }} <br/>
{{ object.description }} <br/>
{{ object.image.url }}<br/>
<img src="{{ object.image.url }}" class='img-fluid'/>
but the {{ object.image.url }}
actually gives me the exact path of the Image which should make sense for the the picture to rendered.