Answer the question
In order to leave comments, you need to log in
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
<div class="result-akcia-yellowblock">
Акция действует до: {{ masters_promo.master_promo_discount }}
<div class="akcia-end">До конца акции: <span class="red-text bb f18"> </span></div>
</div>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question