diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 4410defff..c8447c7d9 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -16,6 +16,7 @@ Changelog
* Fix: Middleware responses during page preview are now properly returned to the user (Matt Westcott)
* Fix: Default text of page links in rich text uses the public page title rather than the admin display title (Andy Chosak)
* Fix: Specific page permission checks are now enforced when viewing a page revision (Andy Chosak)
+ * Fix: `pageurl` and `slugurl` tags no longer fail when `request.site` is `None` (Samir Shah)
2.6.1 (05.08.2019)
diff --git a/docs/releases/2.7.rst b/docs/releases/2.7.rst
index eec138ae7..fdaede9b5 100644
--- a/docs/releases/2.7.rst
+++ b/docs/releases/2.7.rst
@@ -34,6 +34,7 @@ Bug fixes
* Middleware responses during page preview are now properly returned to the user (Matt Westcott)
* Default text of page links in rich text uses the public page title rather than the admin display title (Andy Chosak)
* Specific page permission checks are now enforced when viewing a page revision (Andy Chosak)
+ * ``pageurl`` and ``slugurl`` tags no longer fail when ``request.site`` is ``None`` (Samir Shah)
Upgrade considerations
diff --git a/wagtail/core/templatetags/wagtailcore_tags.py b/wagtail/core/templatetags/wagtailcore_tags.py
index a50cf0668..f2bc28e72 100644
--- a/wagtail/core/templatetags/wagtailcore_tags.py
+++ b/wagtail/core/templatetags/wagtailcore_tags.py
@@ -31,6 +31,10 @@ def pageurl(context, page, fallback=None):
# request.site not available in the current context; fall back on page.url
return page.url
+ if current_site is None:
+ # request.site is set to None; fall back on page.url
+ return page.url
+
# Pass page.relative_url the request object, which may contain a cached copy of
# Site.get_site_root_paths()
# This avoids page.relative_url having to make a database/cache fetch for this list
@@ -48,13 +52,15 @@ def slugurl(context, slug):
that matches the slug on any site.
"""
+ page = None
try:
current_site = context['request'].site
except (KeyError, AttributeError):
# No site object found - allow the fallback below to take place.
- page = None
+ pass
else:
- page = Page.objects.in_site(current_site).filter(slug=slug).first()
+ if current_site is not None:
+ page = Page.objects.in_site(current_site).filter(slug=slug).first()
# If no page is found, fall back to searching the whole tree.
if page is None:
diff --git a/wagtail/core/tests/tests.py b/wagtail/core/tests/tests.py
index 20001ca12..84f861852 100644
--- a/wagtail/core/tests/tests.py
+++ b/wagtail/core/tests/tests.py
@@ -48,6 +48,16 @@ class TestPageUrlTags(TestCase):
result = tpl.render(template.Context({'page': page, 'request': HttpRequest()}))
self.assertIn('Events', result)
+ def test_pageurl_with_null_site_in_request(self):
+ page = Page.objects.get(url_path='/home/events/')
+ tpl = template.Template('''{% load wagtailcore_tags %}{{ page.title }}''')
+
+ # 'request' object in context, but site is None
+ request = HttpRequest()
+ request.site = None
+ result = tpl.render(template.Context({'page': page, 'request': request}))
+ self.assertIn('Events', result)
+
def test_bad_pageurl(self):
tpl = template.Template('''{% load wagtailcore_tags %}{{ page.title }}''')
@@ -96,6 +106,13 @@ class TestPageUrlTags(TestCase):
result = slugurl(template.Context({'request': HttpRequest()}), 'events')
self.assertEqual(result, '/events/')
+ def test_slugurl_with_null_site_in_request(self):
+ # 'request' object in context, but site is None
+ request = HttpRequest()
+ request.site = None
+ result = slugurl(template.Context({'request': request}), 'events')
+ self.assertEqual(result, '/events/')
+
class TestSiteRootPathsCache(TestCase):
fixtures = ['test.json']