Answer the question
In order to leave comments, you need to log in
Images are not displayed in html using Django, but the url of the image in the page code is the image 0 by 0! How to fix it?
When displaying on the site in the html code, I prescribe:
{% for products in product_list %}
<!-- Product -->
<div class="product">
<div class="product_image"><img src="{{ products.img.url }}" alt=""></div>
<div class="product_content">
<div class="product_title"><a href="product.html">{{ products.name }}</a></div>
<div class="product_price">{{ products.cost }}</div>
</div>
</div>
{% endfor %}
class Products(models.Model):
name = models.CharField("Имя продукта", max_length = 150)
img = models.ImageField("Изображение", upload_to = "product/")
description = models.TextField("Описание")
cost = models.IntegerField("цена", default = 0)
in_stock = models.BooleanField("наличие", default=False)
hot = models.BooleanField("Хит", default = False)
sale = models.BooleanField("Скидка", default=False)
draft = models.BooleanField("черновик", default=False)
class ProductView(View):
def get(self, request):
product = Products.objects.all()
return render(request, "products/index.html", {"product_list" : product})
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
.products
{
width: 100%;
background: #FFFFFF;
padding-top: 99px;
z-index: 2;
}
.product
{
width: calc((100% - 90px) / 4);
margin-bottom: 59px;
}
.product_image
{
width: 100%;
}
.product_image img
{
max-width: 100%;
}
.product_content
{
width: 100%;
padding-top: 36px;
padding-bottom: 38px;
}
.product_title a
{
font-size: 18px;
font-weight: 500;
color: #1b1b1b;
line-height: 1.1;
-webkit-transition: all 200ms ease;
-moz-transition: all 200ms ease;
-ms-transition: all 200ms ease;
-o-transition: all 200ms ease;
transition: all 200ms ease;
}
.product_title a:hover
{
color: #e95a5a;
}
.product_price
{
font-size: 16px;
font-weight: 500;
color: #6c6a74;
line-height: 0.75;
margin-top: 13px;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question