Add a label "NEW" to new products in Django eCommerce

PHOTO EMBED

Sat Oct 09 2021 22:08:37 GMT+0000 (Coordinated Universal Time)

Saved by @adk000 #django #ecommerce

// models.py
from django.utils import timezone

class Product(models.Model):
    name = models.CharField(max_length=225)
    created_on = models.DateTimeField(auto_now_add=True)
    
    @property
    def is_new(self):
        now = timezone.now()
        diff = now - self.created_on
        
        if diff.days < 7: # 7 is the number of days
            return True
        else:
            return False  

// in your template
<h4>{{ product.name }}{% if product.is_new %}<span>სიახლე</span>{% endif %}</h4>
content_copyCOPY