How to Make This Django's ImageField Work?

34 Views Asked by At

models.py

`class SpecialOffer(models.Model): productImage = models.ImageField(upload_to="", null=True, blank=True) productName = models.CharField('Product Name', max_length=20)

def __str__(self):
    return self.productName`

views.py

`def homepage(request): specialoffers = SpecialOffer.objects.all()

context = {
    'specialoffers' : specialoffers,
}

return render(request, "index.html", context)`

index.html

{% for product_name in specialoffers %} <img src="{{ product_name.productImage.url }}" alt="" /> {% endfor %}

and the error is that the image i'm uploading through admin sections, is not rendering through my HTML templates. what's wrong with my code?

I tried changing this {{ product_name.productImage.url }} to {{ product_name.productImage }} but is still not working.

I also tried setting up the MEDIA_URL, and MEDIA_ROOT.

0

There are 0 best solutions below