Created
July 25, 2019 19:57
-
-
Save mdaizovi/aad37583d49b1c99f69869370546881d to your computer and use it in GitHub Desktop.
Overriding the django admin template to create Back/Forward (or Next/Previous) buttons (2 of 2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import template | |
from django.contrib.admin.views.main import PAGE_VAR | |
from django.utils.html import format_html | |
register = template.Library() | |
DOT = "." | |
@register.simple_tag | |
def back_url(cl, i): | |
""" | |
Generate url for Back button in admin pagination | |
""" | |
if i == DOT: | |
return "… " | |
elif cl.page_num > 0: | |
return format_html( | |
'<a href="{}">{}</a> ', | |
cl.get_query_string({PAGE_VAR: cl.page_num - 1}), | |
"Back", | |
) | |
else: | |
return "" | |
@register.simple_tag | |
def forward_url(cl, i): | |
""" | |
Generate url for Forward button in admin pagination | |
""" | |
if i == DOT: | |
return "… " | |
elif cl.page_num + 1 < cl.paginator.num_pages: | |
return format_html( | |
'<a href="{}">{}</a> ', | |
cl.get_query_string({PAGE_VAR: cl.page_num + 1}), | |
"Forward", | |
) | |
else: | |
return "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment