Hello guys !
My problem is i have a bootstrap carousel that takes two product images from the database by id. I can restrict their output through slices [::], but for the next carousel to include the next two products, I have to create a new variable in views.py, since I cannot write a slice or the product id I need to the html template, then I have to create 10 more new variables, and a person who then wants to add a product will have to climb into the code. My site is good working, I just can’t find out how to display the product I need for a separate carousel
views.py (main part)
def main_page(request):
return render(request, 'teddy/teddy.html', {})
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
#products = Context({'bear_1': Product.objects.filter(id='4'),
#'bear_2': Product.objects.filter(id='5')})
products = Product.objects.order_by('id')[0:2]
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request,
'teddy/teddy.html',
{'category': category,
'categories': categories,
'products': products,})
models.py
class Category(models.Model):
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True)
class Meta:
ordering = ('name',)
verbose_name = 'Категория'
verbose_name_plural = 'Категории'
def __str__(self):
return self.name
class Product(models.Model):
category = models.ForeignKey(Category, related_name='products',on_delete=models.CASCADE)
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True)
image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
stock = models.PositiveIntegerField()
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('name',)
#index_together = (('id', 'slug'),)
def __str__(self):
return self.name
html:
<!--50cm-->
<div id="carouselFirstLine" class="carousel slide mx-auto d-block col-md-4 col-lg-4" data-ride="carousel" data-interval='false'>
<div class="carousel-inner rounded">
<div class="carousel-item active">
{% for product in products %}
<svg class='bag_1' class="bi bi-cart3" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill="white" d="M0 1.5A.5.5 0 0 1 .5 1H2a.5.5 0 0 1 .485.379L2.89 3H14.5a.5.5 0 0 1 .49.598l-1 5a.5.5 0 0 1-.465.401l-9.397.472L4.415 11H13a.5.5 0 0 1 0 1H4a.5.5 0 0 1-.491-.408L2.01 3.607 1.61 2H.5a.5.5 0 0 1-.5-.5zM3.102 4l.84 4.479 9.144-.459L13.89 4H3.102zM5 12a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm7 0a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm-7 1a1 1 0 1 0 0 2 1 1 0 0 0 0-2zm7 0a1 1 0 1 0 0 2 1 1 0 0 0 0-2z"/>
</svg>
<div class="item">
<a href="{{ product.get_absolute_url }}">
<img src="{% if product.image %}{{ product.image.url }}{% else %}{% static "img/no_image.png" %}{% endif %}">
</a>
</div>
{% endfor %}
<div class="carousel-caption">
<p>Плюшевый мишка "I love You" 50 см(пепельный)</p>
</div>
</div>