0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Revise pagination_nav.html so that linkurl can be a direct URL as well as a named URL route

This commit is contained in:
Matt Westcott 2021-07-29 00:14:18 +01:00 committed by Matt Westcott
parent ca8dd6d007
commit b3adae22a2
2 changed files with 23 additions and 11 deletions

View File

@ -2,23 +2,18 @@
{% load wagtailadmin_tags %}
{% comment %}
HACK: This template expects to be passed a 'linkurl' parameter, containing a URL name
that can be reverse-resolved by the {% url %} tag with no further parameters.
Views that have parameters in their URL can work around this by omitting linkurl,
which will produce a final URL of the form "?q=123", implicitly preserving the current URL path.
Using the {% url ... as ... %} form of the tag ensures that this fails silently,
rather than throwing a NoReverseMatch exception.
Expects a 'linkurl' parameter to use as the base URL, which may be a URL route name (must be
reverse-resolvable with no arguments) or a direct URL path. If omitted, links will be given
an href of the form "?p=123", implicitly preserving the current URL path.
{% endcomment %}
{% if linkurl %}
{% url linkurl as url_to_use %}
{% endif %}
{% resolve_url_if_not_absolute linkurl as url_path %}
<nav class="pagination" aria-label="{% trans 'Pagination' %}">
<p>{% blocktrans with page_num=items.number total_pages=items.paginator.num_pages %}Page {{ page_num }} of {{ total_pages }}.{% endblocktrans %}</p>
<ul>
<li class="prev">
{% if items.has_previous %}
<a href="{{ url_to_use }}{% querystring p=items.previous_page_number %}">
<a href="{{ url_path }}{% querystring p=items.previous_page_number %}">
{% icon name="arrow-left" class_name="default" %}
{% trans 'Previous' %}
</a>
@ -26,7 +21,7 @@
</li>
<li class="next">
{% if items.has_next %}
<a href="{{ url_to_use }}{% querystring p=items.next_page_number %}">
<a href="{{ url_path }}{% querystring p=items.next_page_number %}">
{% trans 'Next' %}
{% icon name="arrow-right" class_name="default" %}
</a>

View File

@ -15,6 +15,7 @@ from django.template.defaultfilters import stringfilter
from django.template.loader import render_to_string
from django.templatetags.static import static
from django.urls import reverse
from django.urls.exceptions import NoReverseMatch
from django.utils import timezone
from django.utils.encoding import force_str
from django.utils.html import avoid_wrapping, format_html, format_html_join
@ -716,3 +717,19 @@ def menu_props(context):
@register.simple_tag
def get_comments_enabled():
return getattr(settings, 'WAGTAILADMIN_COMMENTS_ENABLED', True)
@register.simple_tag
def resolve_url_if_not_absolute(url):
# Used by wagtailadmin/shared/pagination_nav.html - given an input that may be a URL route
# name, or a direct URL path, return it as a direct URL path. On failure (or being passed
# an empty / None value), return empty string
if not url:
return ''
elif url.startswith('/'):
return url
else:
try:
return reverse(url)
except NoReverseMatch:
return ''