Why aren't the images from my media library sh

2020-03-27 07:58发布

enter image description here

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/'

标签: python django
2条回答
我只想做你的唯一
2楼-- · 2020-03-27 08:54

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 in media/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="...">

查看更多
家丑人穷心不美
3楼-- · 2020-03-27 08:58

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:

image = models.ImageField(upload_to='shop/images', default="")

and in your settings.py you have:

STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/

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.

from django.core.files.storage import FileSystemStorage
from django.conf import settings


image_storage = FileSystemStorage(
    # Physical file location ROOT
    location=u'{0}/my_sell/'.format(settings.MEDIA_ROOT),
    # Url for file
    base_url=u'{0}my_sell/'.format(settings.MEDIA_URL),
)


def image_directory_path(instance, filename):
    # file will be uploaded to MEDIA_ROOT/my_sell/picture/<filename>
    return u'picture/{0}'.format(, filename)


class Goods(models.Model):
    pic = models.ImageField(upload_to=image_directory_path, storage=image_storage)

For more information please see: Django image src not found

Hope it helps.

查看更多
登录 后发表回答