Django-折扣后如何显示特定文本不再有效

Django-折扣后如何显示特定文本不再有效,django,python-3.x,Django,Python 3.x,我正在从事一个项目,我想知道当“折扣价格”的有效期到期时,如何显示产品“名称”及其“价格”,只要“折扣价格”有效,我想显示产品“名称”、“价格”和“折扣价格” 型号。py: from django.db import models from datetime import datetime class Products(models.Model): name=models.CharField(max_length=60) photo = models.ImageField(up

我正在从事一个项目,我想知道当“折扣价格”的有效期到期时,如何显示产品“名称”及其“价格”,只要“折扣价格”有效,我想显示产品“名称”、“价格”和“折扣价格”

型号。py:

from django.db import models
from datetime import datetime

class Products(models.Model):
    name=models.CharField(max_length=60)
    photo = models.ImageField(upload_to='photos/', default='photos/none.png',
                              null=True, blank=True)
    description=models.TextField()
    price=models.FloatField()
    discount_price = models.FloatField(blank=True, null=True)
    valid_from = models.DateTimeField(('Valid from'), default=datetime.now)
    valid_until = models.DateTimeField(('Valid until'), blank=True, null=True)
    stock=models.IntegerField(default=0)
{% extends "./base.html" %}
{% load static %}
{% block content %}

<h3 style="text-align:center"> Product list: </h3>
  {% for product in products %}
       <div class="products">
           <div class="item">
               <table >
                   {% if product.discount %}
                       <tr><td> <a class="products" href ="{% url 'entry' produs.id %}">
                           {{products.name}} {{product.discount|cut:".0"}} {{product.price|cut:".0"}}</a>
                   {% else %}
                       <tr><td><a class="products" href ="{% url 'entry' product.id %}"> {{product.name}}  {{product.price|cut:'.0'}} </a>
                   {% endif %}
               </table>
            </div>
       </div>
  {% endfor %}
{% endblock content %}
我的模板index.html:

from django.db import models
from datetime import datetime

class Products(models.Model):
    name=models.CharField(max_length=60)
    photo = models.ImageField(upload_to='photos/', default='photos/none.png',
                              null=True, blank=True)
    description=models.TextField()
    price=models.FloatField()
    discount_price = models.FloatField(blank=True, null=True)
    valid_from = models.DateTimeField(('Valid from'), default=datetime.now)
    valid_until = models.DateTimeField(('Valid until'), blank=True, null=True)
    stock=models.IntegerField(default=0)
{% extends "./base.html" %}
{% load static %}
{% block content %}

<h3 style="text-align:center"> Product list: </h3>
  {% for product in products %}
       <div class="products">
           <div class="item">
               <table >
                   {% if product.discount %}
                       <tr><td> <a class="products" href ="{% url 'entry' produs.id %}">
                           {{products.name}} {{product.discount|cut:".0"}} {{product.price|cut:".0"}}</a>
                   {% else %}
                       <tr><td><a class="products" href ="{% url 'entry' product.id %}"> {{product.name}}  {{product.price|cut:'.0'}} </a>
                   {% endif %}
               </table>
            </div>
       </div>
  {% endfor %}
{% endblock content %}
{%extends./base.html%}
{%load static%}
{%block content%}
产品清单:
{products%中产品的%s}
{%if product.discount%}
{%else%}
{%endif%}
{%endfor%}
{%endblock内容%}

如果折扣仍然有效,您可以创建返回的属性:

from django.db import models
from django.utils import timezone

class Products(models.Model):
    # …

    @property
    def active_discount(self):
        now = timezone.now()
        return self.discount_price and self.valid_from <= now and (self.valid_until is None or now <= self.valid_until)
来自django.db导入模型的

从django.utils导入时区
类别产品(型号.型号):
# …
@财产
def主动折扣(自助):
now=时区。now()

return self.discount\u price和self.valid\u from如果折扣仍然有效,则可以创建返回的属性:

from django.db import models
from django.utils import timezone

class Products(models.Model):
    # …

    @property
    def active_discount(self):
        now = timezone.now()
        return self.discount_price and self.valid_from <= now and (self.valid_until is None or now <= self.valid_until)
来自django.db导入模型的

从django.utils导入时区
类别产品(型号.型号):
# …
@财产
def主动折扣(自助):
now=时区。now()
从返回self.discount\u价格和self.valid\u