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 file…

from django.utils import timezone
from datetime import timedelta

def myview(request):
    now =
    result = Mymodel.objects.aggregate(
        today=models.Count('id', filter=models.Q(,
        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>{{ }} today</p>
<p>{{ result.yesterday }} Yesterday</p>
<p>{{ result.last_7_day }} Last 7 Days</p>


Please enter your comment!
Please enter your name here