Opened 4 years ago

Closed 4 years ago

#26015 closed Bug (fixed)

sitemap view can raise UnboundLocalError

Reported by: Keryn Knight Owned by: Andrew Kuchev
Component: contrib.sitemaps Version: master
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 Changed 4 years ago by Simon Charette

Triage Stage: UnreviewedAccepted

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

comment:2 Changed 4 years ago by Vincenzo Pandolfo

Owner: changed from nobody to Vincenzo Pandolfo
Status: newassigned

comment:3 Changed 4 years ago by Vincenzo Pandolfo

Owner: Vincenzo Pandolfo deleted
Status: assignednew

comment:4 Changed 4 years ago by Tim Graham

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 Changed 4 years ago by Andrew Kuchev

Owner: set to Andrew Kuchev
Status: newassigned

comment:6 Changed 4 years ago by Andrew Kuchev

Has patch: set

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

comment:7 Changed 4 years ago by Tim Graham <timograham@…>

In 5b2e11e5:

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

comment:8 Changed 4 years ago by Tim Graham

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