[Django]-Python โ€“ django: why am I getting this error: AttributeError: 'method_descriptor' object has no attribute 'today'?

69๐Ÿ‘

โœ…

You probably want import datetime, not from datetime import datetime.

date is a class on the datetime module, but it is also a method on the datetime.datetime class.

๐Ÿ‘คAdam Vandenberg

37๐Ÿ‘

The top answer is correct, but if you donโ€™t want to import all of datetime you can write

from datetime import date

and then replace

datetime.date.today()

with

date.today()
๐Ÿ‘คJoe

5๐Ÿ‘

You need do like this one (ipython output)

In [9]: datetime.today().date()
Out[9]: datetime.date(2011, 2, 5)

So need to be

def was_published_today(self):
        return self.pub_date.date() == datetime.today().date()
๐Ÿ‘คrootart

Leave a comment