[3.2.x] Fixed #32648 -- Fixed VariableDoesNotExist rendering sitemaps template.

Backport of 08c60cce3b from main
This commit is contained in:
Arthur Jovart 2021-04-14 14:50:01 +02:00 committed by Carlton Gibson
parent 59cce8237c
commit 65dfb06a1a
4 changed files with 12 additions and 6 deletions

View File

@ -94,6 +94,7 @@ answer newbie questions, and generally made Django that much better:
Aron Podrigal <aronp@guaranteedplus.com>
Artem Gnilov <boobsd@gmail.com>
Arthur <avandorp@gmail.com>
Arthur Jovart <arthur@jovart.com>
Arthur Koziel <http://arthurkoziel.com>
Arthur Rio <arthur.rio44@gmail.com>
Arvis Bickovskis <viestards.lists@gmail.com>

View File

@ -168,13 +168,13 @@ class Sitemap:
'lastmod': lastmod,
'changefreq': self._get('changefreq', item),
'priority': str(priority if priority is not None else ''),
'alternates': [],
}
if self.i18n and self.alternates:
alternates = []
for lang_code in self._languages():
loc = f'{protocol}://{domain}{self._location(item, lang_code)}'
alternates.append({
url_info['alternates'].append({
'location': loc,
'lang_code': lang_code,
})
@ -182,11 +182,10 @@ class Sitemap:
lang_code = settings.LANGUAGE_CODE
loc = f'{protocol}://{domain}{self._location(item, lang_code)}'
loc = loc.replace(f'/{lang_code}/', '/', 1)
alternates.append({
url_info['alternates'].append({
'location': loc,
'lang_code': 'x-default',
})
url_info['alternates'] = alternates
urls.append(url_info)

View File

@ -30,3 +30,6 @@ Bugfixes
* Fixed a regression in Django 3.2 that caused a crash of
:attr:`.ModelAdmin.search_fields` when searching against phrases with
unbalanced quotes (:ticket:`32649`).
* Fixed a bug in Django 3.2 where variable lookup errors were logged rendering
the sitemap template if alternates were not defined (:ticket:`32648`).

View File

@ -255,9 +255,12 @@ class HTTPSitemapTests(SitemapTestsBase):
@override_settings(LANGUAGES=(('en', 'English'), ('pt', 'Portuguese')))
def test_simple_i18n_sitemap_index(self):
"""
A simple i18n sitemap index can be rendered.
A simple i18n sitemap index can be rendered, without logging variable
lookup errors.
"""
response = self.client.get('/simple/i18n.xml')
with self.assertRaisesMessage(AssertionError, 'no logs'):
with self.assertLogs('django.template', 'DEBUG'):
response = self.client.get('/simple/i18n.xml')
expected_content = """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url><loc>{0}/en/i18n/testmodel/{1}/</loc><changefreq>never</changefreq><priority>0.5</priority></url><url><loc>{0}/pt/i18n/testmodel/{1}/</loc><changefreq>never</changefreq><priority>0.5</priority></url>