From c82b6835a009d57417551efe4da4ed677f724699 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Fri, 20 Apr 2018 08:02:29 +0100 Subject: [PATCH] Add a meaningful error message for invalid arguments to pageurl (#4228) --- wagtail/core/templatetags/wagtailcore_tags.py | 3 +++ wagtail/core/tests/tests.py | 6 ++++++ 2 files changed, 9 insertions(+) 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''')