diff --git a/wagtail/core/templatetags/wagtailcore_tags.py b/wagtail/core/templatetags/wagtailcore_tags.py
index a66634b964..7f13ba27a7 100644
--- a/wagtail/core/templatetags/wagtailcore_tags.py
+++ b/wagtail/core/templatetags/wagtailcore_tags.py
@@ -16,6 +16,9 @@ def pageurl(context, page):
Outputs a page's URL as relative (/foo/bar/) if it's within the same site as the
current page, or absolute (http://example.com/foo/bar/) if not.
"""
+ if not hasattr(page, 'relative_url'):
+ raise ValueError("pageurl tag expected a Page object, got %r" % page)
+
try:
current_site = context['request'].site
except (KeyError, AttributeError):
diff --git a/wagtail/core/tests/tests.py b/wagtail/core/tests/tests.py
index a730deb21d..ce8b001c37 100644
--- a/wagtail/core/tests/tests.py
+++ b/wagtail/core/tests/tests.py
@@ -37,6 +37,12 @@ class TestPageUrlTags(TestCase):
result = tpl.render(template.Context({'page': page, 'request': HttpRequest()}))
self.assertIn('Events', result)
+ def test_bad_pageurl(self):
+ tpl = template.Template('''{% load wagtailcore_tags %}{{ page.title }}''')
+
+ with self.assertRaisesRegex(ValueError, "pageurl tag expected a Page object, got None"):
+ tpl.render(template.Context({'page': None}))
+
def test_slugurl_without_request_in_context(self):
tpl = template.Template('''{% load wagtailcore_tags %}Events''')