[英]Django Diff between Dates
I have a project in django and i am trying to render html to pdf. 我在Django有一个项目,我正在尝试将html渲染为pdf。 I'm trying to build a table, and i have two varaibles
date1
and date2
and i need to do a Diff
between date1
and date2
. 我试图建立一个表,我有两个varaibles
date1
和date2
,我需要做一个Diff
之间date1
和date2
。
If the result is more than 20 woriking days
show 1
if not show 0
如果结果超过
20 woriking days
显示1
否则,显示0
MY HTML 我的HTML
{% for item in obj %}
<tr>
<td>
{% if item.date1 - item.date2 > 20 %}
1
{% else %}
0
{% endif %}
</td>
</tr>
{% endfor %}
You can add a method in the model to calculate difference, then use it in the templates/pdf. 您可以在模型中添加一种方法来计算差异,然后在template / pdf中使用它。 For example:
例如:
class SomeView(models.Model):
# .. fields
def date_diff(self):
diff = self.date1 - self.date2 # returns time delta object
return abs(diff.days)
And use it in template: 并在模板中使用它:
{% for item in obj %}
<tr>
<td>
{% if item.date_diff > 20 %}
1
{% else %}
0
{% endif %}
</td>
</tr>
{% endfor %}
you should perform the calculation in the backend and send number of days value while rendering the template 您应该在后端执行计算并在渲染模板时发送天数值
difference=item.date1-item.date2
days=difference.days
and in template 并在模板中
{% if days > 20 %}
1
{% else %}
0
{% endif %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.