Suppose you want to display Data according to filters then you can use these Query for display data for Today, yesterday and last 7 Days…I the below code created_on is your DateTimeField name

Please write this code in your views.py file…

from django.utils import timezone
from datetime import timedelta

def myview(request):
    now = timezone.now()
    result = Mymodel.objects.aggregate(
        total=models.Count('id'),
        today=models.Count('id', filter=models.Q(created_on__date=now.date())),
        yesterday=models.Count('id', filter=models.Q(created_on__date__gte=(now - timedelta(hours=24)).date())),
        last_7_day=models.Count('id', filter=models.Q(created_on__date__gte=(now - timedelta(days=7)).date())),
    )
    context = {'result': result}
    return render(request, 'test.html', context)

and your html file will be something like this…

<p>{{ result.today }} today</p>
<p>{{ result.yesterday }} Yesterday</p>
<p>{{ result.last_7_day }} Last 7 Days</p>

LEAVE A REPLY

Please enter your comment!
Please enter your name here