O
O
ooker2020-11-02 02:24:56
Django
ooker, 2020-11-02 02:24:56

How to write a promotion days counter in django model?

Good afternoon!
I'm trying to make a counter in the model. How else can you do it?
You need to add the current day to the figure and get the end date for the end of the promotion.
model

master_promo_discount = models.IntegerField()
    master_promo_date = models.DateField(editable=False )
   
    class Meta:
        verbose_name_plural = 'СОЗДАТЬ АКЦИЮ'

    def get_sale(self):
        price = int(self.master_promo_price * (100 - self.master_promo_discount) / 100)
        return price

    def save(self, **kwargs):
        from datetime import datetime, timedelta
        d = timedelta(days=self.master_promo_discount)

        if not self.id:
            self.master_promo_date = datetime.today() + d
            super(MasterPromoCreate, self).save()

    def __str__(self):
        return self.master_promo_title

    def get_absolute_url(self):
        return "/search-master/master/%i/" % self.id

html
<div class="result-akcia-yellowblock">
Акция действует до: {{ masters_promo.master_promo_discount }}
<div class="akcia-end">До конца акции: <span class="red-text bb f18"> </span></div>
</div>

Outputs none.
Does anyone know of other working options?
I can throw off 3 two-zero wooden ones on the phone for a working code !!!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2020-11-02
@fifty59nine

You can in views.py, in the function of processing this page, insert the following code:

from datetime import date, timedelta
def index(request):
       enddate = masters_promo.masters_promo_discount + timedelta(days=7)
       return render(request, 'index.html', {'enddate':enddate})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question