Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#23403 closed Bug (fixed)

sitemap crashes if lastmod returns a date (instead of datetime)

Reported by: Igor Koshkarov Owned by: Tim Graham
Component: contrib.sitemaps Version: 1.7
Severity: Release blocker Keywords: date utctimetuple
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

When building a sitemap I receive

Django Version: 	1.7
Exception Type: 	AttributeError
Exception Value: 	'datetime.date' object has no attribute 'utctimetuple'

Exception Location: 	<skipped>/Django/lib/python2.7/site-packages/django/contrib/sitemaps/views.py in sitemap, line 78

Workaround can be done as:

diff --git a/django/contrib/sitemaps/views.py b/django/contrib/sitemaps/views.py
index aa184e9..d74c55b 100644
--- a/django/contrib/sitemaps/views.py
+++ b/django/contrib/sitemaps/views.py
@@ -8,6 +8,7 @@ from django.http import Http404
 from django.template.response import TemplateResponse
 from django.utils import six
 from django.utils.http import http_date
+from datetime import datetime
 
 
 def x_robots_tag(func):
@@ -71,7 +72,7 @@ def sitemap(request, sitemaps, section=None,
             raise Http404("No page '%s'" % page)
     response = TemplateResponse(request, template_name, {'urlset': urls},
                                 content_type=content_type)
-    if hasattr(site, 'latest_lastmod'):
+    if hasattr(site, 'latest_lastmod') and type(site.latest_lastmod) is datetime:
         # if latest_lastmod is defined for site, set header so as
         # ConditionalGetMiddleware is able to send 304 NOT MODIFIED
         response['Last-Modified'] = http_date(

Attachments (1)

sitemap.tgz (22.6 KB) - added by Igor Koshkarov 6 years ago.
added test

Download all attachments as: .zip

Change History (12)

comment:1 Changed 6 years ago by Igor Koshkarov

Owner: changed from nobody to Igor Koshkarov
Status: newassigned

comment:2 Changed 6 years ago by Tim Graham

Description: modified (diff)
Easy pickings: unset
Needs tests: set

Could you please include code to reproduce the issue (ideally as a test for Django's test suite)? Did your code work in Django 1.6?

comment:3 Changed 6 years ago by Igor Koshkarov

This code works fine in Django 1.6.6
The error appeared in Django 1.7
I suppose this error is highly dependent on the type of date_field - see info_dict1 in urls.py

Thanks.

Changed 6 years ago by Igor Koshkarov

Attachment: sitemap.tgz added

added test

comment:4 Changed 6 years ago by Tim Graham

Patch needs improvement: set
Severity: NormalRelease blocker
Summary: Sitemap bugsitemap crashes if lastmod returns a date (instead of datetime)
Triage Stage: UnreviewedAccepted

I think the correct solution would be to use the date for the Last-Modified header, not skip it as the patch in the ticket description does.

Here's the commit where the feature was added:
https://github.com/django/django/commit/8f5533ab250df07ea84f98d39808806e282468a5

comment:5 Changed 6 years ago by Igor Koshkarov

OK, in this case there could be more clear solution, working for both date and datetime:

@@ -75,5 +75,5 @@

# if latest_lastmod is defined for site, set header so as
# ConditionalGetMiddleware is able to send 304 NOT MODIFIED
responseLast-Modified? = http_date(

  • timegm(site.latest_lastmod.utctimetuple()))

+ timegm(site.latest_lastmod.timetuple()))

return response

Thank you!

comment:6 Changed 6 years ago by Tim Graham

Does that change alter the value that's returned if the datetime is time-zone aware? Do you plan to add a regression test for this?

comment:7 Changed 6 years ago by Tim Graham

Owner: changed from Igor Koshkarov to Tim Graham

comment:8 Changed 6 years ago by Tim Graham

Needs tests: unset
Patch needs improvement: unset

comment:9 Changed 6 years ago by Preston Timmons

Triage Stage: AcceptedReady for checkin

comment:10 Changed 6 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 66580fe97c0ec8fe16ce7ff7f75a67c7f2340f83:

Fixed #23403 -- Fixed crash in contrib.sitemaps if lastmod returned a date rather than datetime.

Thanks igorcc for the report.

comment:11 Changed 6 years ago by Tim Graham <timograham@…>

In 7de55fa68e694b84e5ea9c383d87153b5be0d43a:

[1.7.x] Fixed #23403 -- Fixed crash in contrib.sitemaps if lastmod returned a date rather than datetime.

Thanks igorcc for the report and Preston Timmons for review.

Backport of 66580fe97c from master

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