На страницы должны выводиться товары Код страницы
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-sm-4">
<ul class="list-group">
<li class="list-group-item">
<a href="/">Все категории</a>
</li>
<br>
{% for category in categories %}
<li class="list-group-item">
<a href="{{ category.get_absolute_url }}">{{ category.name }}</a>
</li>
{% endfor %}
</ul>
</div>
<div class="col-sm-4">
{% for product in products %}
<div class="card" style="width: 30rem;">
<img class="card-img-top" src="{{product.image.url }}" alt="изображение продукта">
<div class="card-body">
<h5 class="card-title">{{product.title}}</h5>
<p class="card-text">{{product.description | truncatechars:10}}</p>
<a href="{{ product.get_absolute_url }}" class="btn btn-primary">Подробнее</a>
</div>
<small class='text-muted'>{{product.timestamp}}</small>
<small class='text-muted'>{{product.category}}</small>
</div>
{% endfor %}
</div>
<div class="col-sm-4">
</div>
</div>
</div>
{% endblock %}
veiws.py
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = Product.objects.filter(category=category)
context = {
'category': category,
'categories': categories,
'products': products
}
return render(request, 'shop/list.html')
def product_detail(request, id, slug):
product = get_object_or_404(Product, id=id, slug=slug, available=True)
cart_product_form = CartAddProductForm()
context = {
'product': product,
'cart_product_form': cart_product_form
}
return render(request, 'shop/detail.html', context)
models.py
class Category(models.Model):
name = models.CharField(max_length=150, db_index=True)
slug = models.SlugField(max_length=150, unique=True, db_index=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('name',)
verbose_name = 'категория'
verbose_name_plural = 'категории'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_list_by_category', args=[self.slug])
def get_upload_path(instance, filename):
# задаем название файла названием slug`а продукта
filename = instance.slug + '.' + filename.split('.')[1]
return os.path.join('images/', filename)
class Product(models.Model):
category = models.ForeignKey(
Category, related_name='products',
on_delete=models.CASCADE
)
name = models.CharField(max_length=100, db_index=True)
slug = models.SlugField(max_length=100, db_index=True)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
available = models.BooleanField(default=True)
stock = models.PositiveIntegerField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
image = models.ImageField(upload_to=get_upload_path, blank=True)
class Meta:
ordering = ('name',)
index_together = (('id', 'slug'),)
verbose_name = 'Продукт'
verbose_name_plural = 'Продукты'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_detail', args=[self.id, self.slug])
Честно скажу, что на энтузиазме внедряю заимствованный код. Очень надеюсь на помощь!
product_list
вы объявилиcontext
, но не передали его рендеру. Вероятно, дело в этом.