0

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.

This is the the result output that I was telling about, that I'm getting the image url but I can not display the Image

1 Answers1

0
<img src="/media/{{ object.image }}" alt='xyz'>
Mr.k1n1
  • 74
  • 1
  • 11
  • When I do this (```xyz``` the code same time render the alt='xyz'. When I add .url extension (```xyz``` I get the same outcome as shown in the picture that I uploaded –  Apr 19 '20 at 06:48
  • upload your project in GitHub and send a link to me. – Mr.k1n1 Apr 19 '20 at 07:40
  • 2
    Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes. – Mark Rotteveel Apr 19 '20 at 09:54
  • @Mr.kn1 here is the link to the project on github https://github.com/CrazyTeddy1/eCommerce –  Apr 19 '20 at 13:51
  • I can't understand your code but don't worry, go to this link and see how to render image Link = https://drive.google.com/file/d/1BAw8Me4HRf5zY6jLjyzGRXbJCIywGtcS/view – Mr.k1n1 Apr 19 '20 at 16:31