2017-07-12 26 views
0

Ich bin mit dem Problem konfrontiert, Bilddatei im Django E-Commerce-Projekt zu zeigen.Kann jemand in dieser Ausgabe helfen?Bild zeigt nicht in Django Vorlage

home.html

{% for product in products %} 
    <div class='col-sm-4'> 
     <div class="thumbnail"> 
       {% if product.productimage_set.all %} 
         {% for item in product.productimage_set.all %} 
         <img class='img-responsive' src="{{ MEDIA_URL }}{{ item.image}}" />       
         {% endfor %} 

       {% else %} 
       <img class='img-responsive' src="{% static "img/placeholder.svg" %}" /> 
       {% endif %} 

       <div class="caption"> 
       <a href='{{ product.get_absolute_url }}'> <h3>{{ product.title }}</h3></a> 
       <p>{{ product.description|truncatewords:15}}</p> 
       <p><a href="{{ product.get_absolute_url }}" class="btn btn-primary" role="button">View</a> <a href="#" class="btn btn-default" role="button">Button</a></p> 
       </div> 
     </div> 
    </div> {% endfor %} 

model.py

class ProductImage(models.Model): 
product = models.ForeignKey(Product) 
image = models.ImageField(upload_to='products/images/') 
featured = models.BooleanField(default=False) 
thumbnail = models.BooleanField(default=False) 
active = models.BooleanField(default=True) 
updated = models.DateTimeField(auto_now_add=False, auto_now=True) 

def __unicode__(self): 
    return self.product.title 

settings.py

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

Antwort

2
<img class='img-responsive' src="{{ MEDIA_URL }}{{ item.image.url }}" /> 

oder

und in Haupt urls.py

from django.conf import settings 
from django.conf.urls.static import static 


urlpatterns = [ 
    # ... the rest of your URLconf goes here ... 
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 
+0

Nach der Änderung von Ihrem Vorschlag zeigt noch nicht das Bild. – jisan

+0

ok Über welches Bild redest du? Es gibt 2 Bilder – Exprator

+0

jisan