0

I am trying out an ecommerce website where list of products are input in the admin section, everything works well aside the fact that the product image does not display in browser. Have check all the codes and everything seems ok. Displaying this message in my terminal "GET /media/photos/buy-1.jpg HTTP/1.1" 404 4187 Not Found: /media/photos/buy-2.jpg

`HTML
<h2 class="title">Latest Products</h2>
  <div class="row">
    {% for item in object_list %}
    <div class="col-4">
      <div>
        <a href="{{ item.get_absolute_url }}">
          {% if item.photo %}
            <img src="{{  item.photo.url }}" alt="" />
          {% else %}
            <img src="{% static 'images/product-1.jpg' %}" alt=""/>
          {% endif %}
          <h4>{{  item.title }}</h4>
        </a>
        <a href="{{ item.get_absolute_url }}" >
          <h6>{{ item.get_category_display }}</h6>
        </a>
      </div>
      <div class="rating">
        <i class="fa fa-star"></i>
        <i class="fa fa-star"></i>
        <i class="fa fa-star"></i>
        <i class="fa fa-star"></i>
        <i class="fa fa-star-o"></i>
      </div>

      <p class='lead'>
        {% if item.d_price %}
        <del><span>${{ item.price }}</span></del>
        <span>${{ item.d_price }}</span>
        {% else %}
        <p>${{ item.price }}</p>
        {% endif %}
      </p>
    </div>
    {% endfor %}

  </div>`

Model fie

`class Item (models.Model):
title = models.CharField(max_length=100)
price = models.FloatField()
d_price = models.FloatField(blank=True, null=True)
tax = models.FloatField(blank=True, null=True)
category = models.CharField(choices=CATEGORY_CHOICES, max_length=2)
label = models.CharField(choices=LABEL_CHOICES, max_length=1)
slug = models.SlugField()
description = models.TextField()
photo = models.ImageField(upload_to='photos', null=False, blank=False)

def __str__(self):
    return self.title

def get_absolute_url(self):
    return reverse('base:product_details', kwargs={
        'slug': self.slug
    })

def get_add_to_cart_url(self):
    return reverse('base:add_to_cart', kwargs={
        'slug': self.slug
    })

def get_remove_from_cart_url(self):
    return reverse('base:remove_from_cart', kwargs={
        'slug': self.slug
    })
    
@property
def get_photo_url(self):
    if self.photo and hasattr(self.photo, 'url'):
        return self.photo.url`
  • This can have multiple causes: webserver settings, misconfigured `settings.py`, missing `urls.py` paths. You can try to replace `.url` with `.path` which solves such kind of problems in some special cases. Otherwise you have to extend your answer. – hanspeters205 Jan 11 '23 at 12:45
  • is `DEBUG = False` in your project `settings.py`? If yes, then this was the problem, django runserver won't serve media when `DEBUG = False`, turn it on with `DEBUG = True`! – whoami Jan 12 '23 at 00:18
  • Thank you guys, solved the problem. Forgot to add... ** += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ...in my urls.py ** – Chicharito Forset Jan 12 '23 at 14:49

0 Answers0