CMP
CMP

Reputation: 435

python queryset date filter not change date to current date

My queryset filter did return the correct filtered data on the first date that I wrote the code but after that the date is stuck to that specific date and does not bring up the filtered information based on the current date.

What am I doing wrong?

today = datetime.date.today()
todaydate = today
url(r'^maanta/', ListView.as_view(
        queryset= Article.objects.filter(pub_date__startswith=todaydate),
        template_name="myarticle.html")),

Upvotes: 0

Views: 122

Answers (1)

arie
arie

Reputation: 18972

If you assign the date like you did on the module level, it is only evaluated once.

The trick is to use a lambda function:

todaydate = lambda: datetime.date.today()

And to adjust your lookup to:

Article.objects.filter(pub_date__startswith=todaydate())

Upvotes: 2

Related Questions