Django не может найти фото в media/products/1.jpg (например).
Хотя само фото там находится.
Вот вам код:
models.py
class Mahsulot(models.Model):
quantity = models.BooleanField(default=True, verbose_name='Mahsulot mavjudligi')
title = models.CharField(max_length=255, verbose_name='Mahsulotni nomi')
price = models.FloatField(verbose_name='narxi')
description = models.TextField(default='Здесь скоро будет описание', verbose_name='Tavsiflar')
category = models.ForeignKey(Kategoriya,
on_delete=models.CASCADE,
related_name='products',
verbose_name='Mahsulot kategoriyasi')
slug = models.SlugField(unique=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, verbose_name='Saytga qoshilgan vaqti')
def get_absolute_url(self):
return reverse('product_detail', kwargs={'slug': self.slug})
def get_first_photo(self):
return self.images.all()[0].image.url
def __str__(self):
return self.title
def __repr__(self):
return f'Товар: pk={self.pk}, title={self.title}, price={self.price}'
class Meta:
verbose_name = 'Mahsulot'
verbose_name_plural = 'Mahsulotlar'
class Gallereya(models.Model):
image = models.ImageField(upload_to='products/', verbose_name='Rasm')
product = models.ForeignKey(Mahsulot, on_delete=models.CASCADE, related_name='images')
class Meta:
verbose_name = 'Rasm'
verbose_name_plural = 'Rasmlar'
class Razmerlar(models.Model):
product = models.ForeignKey(Mahsulot, on_delete=models.CASCADE, related_name='sizes')
M = models.BooleanField(default=True, verbose_name='M')
L = models.BooleanField(default=True, verbose_name='L')
XXL = models.BooleanField(default=True, verbose_name='2XL')
XXXL = models.BooleanField(default=True, verbose_name='3XL')
XXXXL = models.BooleanField(default=True, verbose_name='4XL')
class Meta:
verbose_name = 'Razmer'
verbose_name_plural = 'Razmerlar'
admin.py
@admin.register(Mahsulot)
class ProductAdmin(admin.ModelAdmin):
list_display = ('pk', 'quantity', 'title', 'price',
'description', 'category', 'slug', 'created_at', 'Rasm')
list_editable = ('quantity', 'price', 'description')
list_display_links = ('pk',)
inlines = [GalleryInline, SizesInline]
prepopulated_fields = {'slug': ('title',)}
list_filter = ('title', 'price')
def Rasm(self, obj):
if obj.images:
# return mark_safe(f'<img src="{obj.images.all()[0].image.url}" width="75">')
return mark_safe(f'<img src="https://edgestile.com/public/users/23196/works/work1394124926.jpg" width="75">') # Ошибка тут
В настройках все ок, но вот на всякий случай:
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'static'
STATICFILES_DIRS = [
BASE_DIR / 'store/static'
]
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'