0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Add a meaningful error message for invalid arguments to pageurl (#4228)

This commit is contained in:
Matt Westcott 2018-04-20 08:02:29 +01:00 committed by Mike Dingjan
parent 3bd3275add
commit c82b6835a0
2 changed files with 9 additions and 0 deletions

View File

@ -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):

View File

@ -37,6 +37,12 @@ class TestPageUrlTags(TestCase):
result = tpl.render(template.Context({'page': page, 'request': HttpRequest()}))
self.assertIn('<a href="/events/">Events</a>', result)
def test_bad_pageurl(self):
tpl = template.Template('''{% load wagtailcore_tags %}<a href="{% pageurl page %}">{{ page.title }}</a>''')
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 %}<a href="{% slugurl 'events' %}">Events</a>''')