V
V
Vladislav ...2021-04-29 11:58:28
Django
Vladislav ..., 2021-04-29 11:58:28

How to display pictures, colors and sizes in a template?

Good afternoon! I'm trying to make a store, I can't figure out how to display pictures, colors and sizes in a template.
The admin panel displays the addition of pictures of colors and sizes.
The pictures on each product will be different, and the colors and sizes on some will be the same.
models.py

class Color(models.Model):
    name = models.CharField('Название категории', max_length=100, blank=True)
    color = ColorField('Цвет', default='#FF0000')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'Цвет'
        verbose_name_plural = 'Цвета'


class Size(models.Model):
    name = models.CharField('Название', help_text='например XL', max_length=50)
    width = models.IntegerField('Ширина', help_text='например 60', )
    height = models.IntegerField('Высота', help_text='например 90', )
    length = models.IntegerField('Длина', help_text='например 90')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'Размер'
        verbose_name_plural = 'Размеры'


class Product(models.Model):
    title = models.CharField('Название', max_length=100)
    slug = models.SlugField('URL', max_length=100)
    text = models.TextField('Короткое описание', help_text='введите короткое описание:')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='Цена')
    color = models.ManyToManyField(Color, related_name='colors', blank=True, verbose_name='Цвет')
    size = models.ManyToManyField(Size, related_name='sizes', blank=True, verbose_name='Размер')

    def __str__(self):
        return self.title

    def get_gallery(self):
        return self.category.image.url

class Gallery(models.Model):
    image = models.ImageField(upload_to='gallery')
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='images')

views.py
class ProductDetail(DetailView):
    model = Product
    template_name = 'shop/detail.html'

class GalleryInline(admin.TabularInline):
    fk_name = 'product'
    model = Gallery
    extra = 1

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    inlines = [GalleryInline, ]
    prepopulated_fields = {'slug': ('title',)}

To see pictures and colors like this
608a7b7bc2778548310197.jpeg

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question