Skip to content

Instantly share code, notes, and snippets.

@mdaizovi
Created July 25, 2019 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdaizovi/aad37583d49b1c99f69869370546881d to your computer and use it in GitHub Desktop.
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)
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