Django admin: adding pagination links in list of objects to top

元气小坏坏 提交于 2021-02-05 05:23:12

问题


Is it possible to have the pagination links that appear at the bottom of a list of objects in Django's admin interface at the top as well?

Can this be done without changing the admin templates? I suspect not, given the lack of a ModelAdmin option, but thought I'd see if anyone had done this before I dug into the template code.

I really, really don't want to have to copy and paste change_list.html into a new file, just so I can add a pagination line - that'll make changing Django versions painful, since I'll have to check if anything's changed in that file, and re-apply my change.


回答1:


Do not copy change_list.html, instead create a new template that extends it:

{% extends "admin/change_list.html" %}

{% block result_list %}
      {% block pagination %} {{ block.super }} {% endblock %} <!-- pagination -->
      {{ block.super }}  <!-- rest of results list -->
{% endblock %}

Then pass the new template's name to ModelAdmin in change_list_template attribute - doc here.




回答2:


The source code implementing the django admin template for change_list.html has a content block so if you create a file change_list.html under 'admin' folder in your templates directory and add this:

{% extends "admin/change_list.html" %}
{# added pagination to top as well as bottom #}
{% block content %}{% pagination cl %}{{ block.super }}{% endblock %}

it should do the trick!



来源:https://stackoverflow.com/questions/4067712/django-admin-adding-pagination-links-in-list-of-objects-to-top

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!