Opened 19 years ago

Closed 19 years ago

Last modified 17 years ago

#183 closed defect (fixed)

TypeError: can't compare datetime.datetime to datetime.date

Reported by: espen@… Owned by: Adrian Holovaty
Component: Template system Version:
Severity: normal Keywords:
Cc: moof@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Error:

There's been an error:
Traceback (most recent call last):

  File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py", line 63, in get_response
    return callback(request, **param_dict)

  File "/usr/lib/python2.4/site-packages/django/views/generic/date_based.py", line 108, in archive_month
    if date >= now:

TypeError: can't compare datetime.datetime to datetime.date

Change History (3)

comment:1 by rmunn@…, 19 years ago

The same problem exists in archive_day(). Furthermore, there's a logic bug in the "is the month in the past?" check in archive_month(). It checks whether date >= now -- but date is constructed from passing just a year and month to time.strptime, which always fills in "1" for the day if the day is missing. Therefore, if today is July 25th, 2005, date will be 2005-07-01 and now will be 2005-07-25, so the check will return False. But we wanted it to return True in this scenario, because the month being examined does contain today and so we do want to add "<= (now)" to the SQL clause. Therefore, the check should really be "if last_day >= now".

The following patch will fix all three of these bugs:

Index: django/views/generic/date_based.py
===================================================================
--- django/views/generic/date_based.py  (revision 304)
+++ django/views/generic/date_based.py  (working copy)
@@ -108,7 +108,7 @@
             break
     lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
     # Only bother to check current date if the month isn't in the past.
-    if date >= now:
+    if last_day >= now.date():
         lookup_kwargs['%s__lte' % date_field] = now
     lookup_kwargs.update(extra_lookup_kwargs)
     object_list = mod.get_list(**lookup_kwargs)
@@ -152,7 +152,7 @@
         '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)),
     }
     # Only bother to check current date if the date isn't in the past.
-    if date >= now:
+    if date >= now.date():
         lookup_kwargs['%s__lte' % date_field] = now
     lookup_kwargs.update(extra_lookup_kwargs)
     object_list = mod.get_list(**lookup_kwargs)

comment:2 by Moof <moof@…>, 19 years ago

Cc: moof@… added

comment:3 by Jacob, 19 years ago

Resolution: fixed
Status: newclosed

(In [308]) Fixed #196: date-based generic views now have a "use_numeric_months" option if you'd like to use numeric months in the urls. Also fixed #183 while I was at it.

Note: See TracTickets for help on using tickets.
Back to Top