How to make a date filter in Django

Asked

Viewed 229 times

1

I need to do a filter on the views where you bring me the date of the last 30 days. And I’m not sure how to do that.

1 answer

0

Try the following:

from datetime import datetime
from datetime import timedelta
from django.views import generic

class SuaListView(generic.ListView):

    queryset = Foo.objects.filter(creation_date__gte=datetime.today()-timedelta(days=30))
    context_object_name = 'foo'
    template_name = 'your_template.html'

This will return all results from the last 30 days in a Listview.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.