Added fallback to pageurl (#4603)

pull/5137/head
Arthur Holzner 2018-06-12 14:03:52 +02:00 zatwierdzone przez Matt Westcott
rodzic fdbd7ca2d3
commit 6f6f9b8e46
6 zmienionych plików z 39 dodań i 2 usunięć

Wyświetl plik

@ -18,6 +18,7 @@ Changelog
* Add Learn Wagtail to third-party tutorials in documentation (Matt Westcott)
* Add a Django setting `TAG_LIMIT` to limit number of tags that can be added to any taggit model (Mani)
* Added instructions on how to generate urls for `ModelAdmin` to documentation (LB (Ben Johnston), Andy Babic)
* Added option to specify a fallback URL on `{% pageurl %}` (Arthur Holzner)
* Fix: Set `SERVER_PORT` to 443 in `Page.dummy_request()` for HTTPS sites (Sergey Fedoseev)
* Fix: Include port number in `Host` header of `Page.dummy_request()` (Sergey Fedoseev)
* Fix: Validation error messages in `InlinePanel` no longer count towards `max_num` when disabling the 'add' button (Todd Dembrey, Thibaud Colas)

Wyświetl plik

@ -28,6 +28,7 @@ Other features
* Add Learn Wagtail to third-party tutorials in documentation (Matt Westcott)
* Add a Django setting ``TAG_LIMIT`` to limit number of tags that can be added to any taggit model (Mani)
* Added instructions on how to generate urls for ``ModelAdmin`` to documentation (LB (Ben Johnston), Andy Babic)
* Added option to specify a fallback URL on ``{% pageurl %}`` (Arthur Holzner)
Bug fixes

Wyświetl plik

@ -160,7 +160,23 @@ Takes a Page object and returns a relative URL (``/foo/bar/``) if within the sam
{% load wagtailcore_tags %}
...
<a href="{% pageurl page.blog_page %}">
<a href="{% pageurl page.get_parent %}">Back to index</a>
A ``fallback`` keyword argument can be provided - this should be a URL route name that takes no parameters, and will be used as a substitute URL when the passed page is ``None``.
.. code-block:: html+django
{% load wagtailcore_tags %}
{% for publication in page.related_publications.all %}
<li>
<a href="{% pageurl publication.detail_page fallback='coming_soon' %}">
{{ publication.title }}
</a>
</li>
{% endfor %}
.. _slugurl_tag:

Wyświetl plik

@ -1,4 +1,5 @@
from django import template
from django.shortcuts import reverse
from django.template.defaulttags import token_kwargs
from django.utils.encoding import force_text
from django.utils.safestring import mark_safe
@ -12,11 +13,15 @@ register = template.Library()
@register.simple_tag(takes_context=True)
def pageurl(context, page):
def pageurl(context, page, fallback=None):
"""
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 kwargs contains a fallback view name and page is None, the fallback view url will be returned.
"""
if page is None and fallback:
return reverse(fallback)
if not hasattr(page, 'relative_url'):
raise ValueError("pageurl tag expected a Page object, got %r" % page)

Wyświetl plik

@ -2,6 +2,7 @@ from django import template
from django.core.cache import cache
from django.http import HttpRequest
from django.test import TestCase
from django.urls.exceptions import NoReverseMatch
from django.utils.safestring import SafeText
from wagtail.core.models import Page, Site
@ -19,6 +20,16 @@ class TestPageUrlTags(TestCase):
self.assertContains(response,
'<a href="/events/christmas/">Christmas</a>')
def test_pageurl_fallback(self):
tpl = template.Template('''{% load wagtailcore_tags %}<a href="{% pageurl page fallback='fallback' %}">Fallback</a>''')
result = tpl.render(template.Context({'page': None}))
self.assertIn('<a href="/fallback/">Fallback</a>', result)
def test_pageurl_fallback_without_valid_fallback(self):
tpl = template.Template('''{% load wagtailcore_tags %}<a href="{% pageurl page fallback='not-existing-endpoint' %}">Fallback</a>''')
with self.assertRaises(NoReverseMatch):
tpl.render(template.Context({'page': None}))
def test_slugurl_tag(self):
response = self.client.get('/events/christmas/')
self.assertEqual(response.status_code, 200)

Wyświetl plik

@ -1,4 +1,5 @@
from django.conf.urls import include, url
from django.http import HttpResponse
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.api.v2.endpoints import PagesAPIEndpoint
@ -37,6 +38,8 @@ urlpatterns = [
url(r'^testapp/', include(testapp_urls)),
url(r'^fallback/', lambda: HttpResponse('ok'), name='fallback'),
# For anything not caught by a more specific rule above, hand over to
# Wagtail's serving mechanism
url(r'', include(wagtail_urls)),