Opened 9 years ago

Closed 9 years ago

#26015 closed Bug (fixed)

sitemap view can raise UnboundLocalError

Reported by: Keryn Knight Owned by: Andrew Kuchev
Component: contrib.sitemaps Version: dev
Severity: Normal Keywords:
Cc: django@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Given the following urlconf definition:

from django.contrib.sitemaps.views import sitemap
url(r'^sitemap.xml$', sitemap, {'sitemaps': {}}, name='is_broken')

any attempt to load that URL will raise:

UnboundLocalError at /sitemap.xml
local variable 'site' referenced before assignment

because if hasattr(site, 'latest_lastmod') attempts to access site which is only ever set if for site in maps yields at least one iteration.

It should instead return an empty urlset, or possibly raise an Http404 with an appropriate debug message.

Change History (8)

comment:1 by Simon Charette, 9 years ago

Triage Stage: UnreviewedAccepted

The fix proposed for #25989 should also fix this ticket.

comment:2 by Vincenzo Pandolfo, 9 years ago

Owner: changed from nobody to Vincenzo Pandolfo
Status: newassigned

comment:3 by Vincenzo Pandolfo, 9 years ago

Owner: Vincenzo Pandolfo removed
Status: assignednew

comment:4 by Tim Graham, 9 years ago

Easy pickings: set

While the related ticket is resolved and this should be fixed, it would be useful to add a regression test for this case.

comment:5 by Andrew Kuchev, 9 years ago

Owner: set to Andrew Kuchev
Status: newassigned

comment:6 by Andrew Kuchev, 9 years ago

Has patch: set

I've added a PR with regression test: https://github.com/django/django/pull/6033

comment:7 by Tim Graham <timograham@…>, 9 years ago

In 5b2e11e5:

Refs #26015 -- Added regression test for an empty sitemap.

comment:8 by Tim Graham, 9 years ago

Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.
Back to Top