I included the images in my database and store the images in the media directory. When I access my data from my database all information is loaded but my images are not showing. Can someone help me out?
This is my index.html file:
{% block body %}
{% load static %}
<div class="container">
<h1>Men Fashion</h1>
<div id="demo" class="carousel slide my-3" data-ride="carousel">
<ul class="carousel-indicators">
<li data-target="#demo" data-slide-to="0" class="active"></li>
{% for i in range %}
<li data-target="#demo" data-slide-to="{{i}}" ></li>
{% endfor %}
</ul>
<!--Slideshow starts here -->
<div class="container carousel-inner no-padding">
<div class="carousel-item active">
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="card" style="width: 18rem;">
{% load static %}
<img src='/media/{{product.0.image}}' class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{product.0.product_name}}</h5>
<p class="card-text">{{product.0.desc}}</p>
<a href="#" class="btn btn-primary">Add to Cart</a>
</div>
</div>
</div>
{% for i in product|slice:"1:"%}
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="card" style="width: 18rem;">
<img src='/media/{{i.image}}' class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{i.product_name}}</h5>
<p class="card-text">{{i.desc}}</p>
<a href="#" class="btn btn-primary">Add To Cart</a>
</div>
</div>
</div>
{% if forloop.counter|divisibleby:3 and forloop.counter > 0 and not forloop.last %}
</div><div class="carousel-item">
{% endif %}
{% endfor %}
</div>
</div>
</div>
<!-- left and right controls for the slide -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
{% endblock %}
</div>
This is my models.py file which I included in my project:
from django.db import models
# Create your models here.
class Product(models.Model):
product_id = models.AutoField
product_name = models.CharField(max_length=50)
category = models.CharField(max_length=50, default="")
subcategory = models.CharField(max_length=50, default="")
price = models.IntegerField(default=0)
desc = models.CharField(max_length=300)
pub_date = models.DateField()
image = models.ImageField(upload_to='shop/images', default="")
def __str__(self):
return self.product_name
This is my setting.py file:
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
First off, you should double-check where your images are actually being uploaded to.
Based on the ImageField, I'm guessing you want to store your product images to media/shop/images. In your template, however, you call
/media/{{product.0.image}}
, which will look for images inmedia/media/shop/images
.Secondly, remember that searches for the path to the image, not just the image itself. So try this:
<img src="{{product.0.image.url }}" class="card-img-top" alt="...">
I do not know to much about this but.. Please have a look here: Django image src not found
Similar problem to you, corrected by code. In your
model.py
code, you say:and in your
settings.py
you have:I do not see where you declare a path to your images. Try this code for your
settings.py
and see what works for you.For more information please see: Django image src not found
Hope it helps.