改变表格中字段的颜色,取决于用户支付剩余的时间,使用 Django。

huangapple go评论65阅读模式
英文:

Change color of a field in a table depending on how much time is left for the user to pay with Django

问题

    def payment_yellow(self):
        return date.today() - self.payment_date

    def payment_red(self):
        return date.today()
{% if obj.payment_date <= obj.payment_red %}
    <div class="p-3 mb-2 bg-danger text-white">{{ obj.payment_date }}</div>
{% elif obj.payment_yellow.days >= 7 %}
    <div class="p-3 mb-2 bg-warning text-white">{{ obj.payment_date }}</div>
{% else %}
    {{ obj.payment_date }}
{% endif %}
英文:

I am trying to change the color of a field in a table depending on how much time is left for the user to pay, if there are 7 days to pay it should change to yellow and if it is already the date of payment or already had to have paid it should change to red, the one that changes to red I got it, but I can't get it to change to yellow when there are 7 days to pay.

This is my model and the def

class payments (models.Model):
    client = models.OneToOneField(HealthQuests, on_delete=models.CASCADE, null=True, blank=True, verbose_name= &#39;Cliente&#39;)
    date = models.DateField(default=datetime.date.today, verbose_name= &#39;Fecha de facturaci&#243;n&#39;)
    payment_date = models.DateField(default=datetime.date.today, verbose_name= &#39;Fecha de pago&#39;)
    amount = models.FloatField(verbose_name=&#39;Monto de pago&#39;, default=0)

    def payment_yellow (self):
        return date.today() - payments.payment_date

    def payment_red (self):
        return date.today()

And my html and the if

&lt;tbody&gt;
            {% for obj in object_list %}
                &lt;tr&gt;
                    &lt;td&gt;&lt;a href=&quot;update_payments/{{ obj.id }}&quot; class=&quot;btn&quot;&gt;{{ obj.client }}&lt;/a&gt;&lt;/td&gt;
                    &lt;td&gt;{{ obj.date }}&lt;/td&gt;
                    &lt;td&gt;
                    {% if obj.payment_date &lt;= obj.payment_red %}
                        &lt;div class=&quot;p-3 mb-2 bg-danger text-white&quot;&gt;{{ obj.payment_date }}&lt;/div&gt;
                    {% elif obj.payment_yellow &gt;= 7 %}
                        &lt;div class=&quot;p-3 mb-2 bg-warning text-white&quot;&gt;{{ obj.payment_date }}&lt;/div&gt;
                    {% else %}
                        {{ obj.payment_date }}
                    {% endif %}
                    &lt;/td&gt;
                    &lt;td&gt;{{ obj.amount }}&lt;/td&gt;
                &lt;/tr&gt;
            {% endfor %}
        &lt;/tbody&gt;

答案1

得分: 1

因为您在模板中硬编码了条件检查为 7,所以函数返回当前日期与付款日期之间的时间间隔 timedelta

另外,应该是 self.payment_date 而不是 payments.payment_date

所以目前您应该提取天数来使您的代码在 payment_yellow() 方法中工作,如下所示:

def payment_yellow(self):
    return (self.payment_date - date.today()).days

注意: Django 模型通常以 PascalCase 编写,不需要添加 s 作为后缀,因为默认会添加,所以最好将其命名为 Payment,而不是 <s>payments</s>

英文:

It is because you hardcoded the condition check to 7 in the template, the function returns timedelta between current date and the payment date.

Also, it should be self.payment_date not payments.payment_date.

So currently you should extract days to make your code work in payment_yellow() method so:

def payment_yellow(self):
    return (self.payment_date - date.today()).days

> Note: Django models are generally written in PascalCase and don't require s to be added as suffix, as it is by default added, so it is better to name it as Payment not <s>payments</s>.

huangapple
  • 本文由 发表于 2023年2月8日 23:14:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定