Q:

How do I create a date range filter in a class based view?

My class based view in "views.py"
class PostListView(ListView):
    model = Post.objects.filter(created__range=["2020-03-09", "2020-03-31"])
    template_name = 'main/problems.html'
    context_object_name = 'posts'
    ordering = ['-created']

I have a variable "created" for when the post is created but dont know how to filter the post within a range.

A:

The .model attribute [Django-doc] refers to the model class, but you can specify the .queryset attribute [Django-doc]:
class PostListView(ListView):
    queryset = Post.objects.filter(created__range=['2020-03-09', '2020-03-31'])
    template_name = 'main/problems.html'
    context_object_name = 'posts'
    ordering = ['-created']

