'Display only days on django template
I'm trying to display the difference between 2 giving dates on django, and i've managed to make it, but now i'm strugling to display only the days, without the time, is there any filter that i can use?
My html template:
<a href="{% url 'edit_contract' contract.id %}">
{% if contract.status == 'PN' %}
{{ today |sub:contract.starting_date }}
{% else %}
TODO
{% endif %}
</a>
My view:
@login_required
def contract_list(request):
contracts = Contract.objects.filter(user=request.user)
total_contracts_value = Contract.objects.filter(user=request.user).aggregate(sum=Sum('value'))['sum'] or 0
contracts_count = Contract.objects.filter(user=request.user).count()
today = date.today()
return render(request, 'list_contract.html', {'contracts': contracts,
'total_contracts_value': total_contracts_value,
'contracts_count': contracts_count, 'today':today})
Solution 1:[1]
Days since start is a property of your contract, so you could create an actual property in the Contract model
from datetime import date
from django.db import models
class Contract(models.Model):
...
@property
def days_since_start(self):
today = date.today()
result = today - self.start_date
return result.days
then refer to the property in your template
{% if contract.status == 'PN' %}
{{ contract.days_since_start }}
{% else %}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SamSparx |

