我想为我的管理面板实现自定义分页。
我的网址如下所示:http://localhost:8000/admin/items/?group_id=20
在这个 URL 上,我做了一些工作来使用参数 group_id
过滤结果。 (通过覆盖 get_changelist
方法)。
页面结果是正确的,问题是我的分页结果是这样的 http://localhost:8000/admin/items/?p=1
而我希望 URL 为 http://localhost:8000/admin/items/?group_id=20&p=1
并保留参数。
基本上我想要与 How to paginate Django with other get variables? 相同的结果但使用 Django 管理员。
如何将参数与分页一起保留?
我试过覆盖 pagination.html 文件但没有任何成功。
谢谢你。
编辑
我试过覆盖 pagination.html 但 request.GET.items
仍然是空的(即使我的设置文件配置良好)
{% load admin_list %}
{% load i18n %}
{% load content_extras %}
<p class="paginator">
{% if pagination_required %}
{% for i in page_range %}
<a href="?p={{ i }}{% for key, value in request.GET.items %}{% if key != 'p' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">{{ i }}</a>
{% endfor %}
{% endif %}
{{ cl.result_count }} {% if cl.result_count == 1 %}{{ cl.opts.verbose_name }}{% else %}{{ cl.opts.verbose_name_plural }}{% endif %}
{% if show_all_url %}<a href="{{ show_all_url }}" class="showall">{% trans 'Show all' %}</a>{% endif %}
{% if cl.formset and cl.result_count %}<input type="submit" name="_save" class="default" value="{% trans 'Save' %}">{% endif %}
</p>
最佳答案
找到解决方案:
1/覆盖 changelist_view
在 admin.py
并传递额外的数据
def changelist_view(self, request, extra_context=""):
response = super(ItemAdmin, self).changelist_view(request, extra_context)
group_id = request.GET.get('group_id', None)
if group_id:
extra_context = {
'group_id': group_id,
}
response.context_data.update(extra_context)
return TemplateResponse(request, "admin/changelist.html", response.context_data)
2/创建 changelist.html
基于 django 管理的文件 template (复制粘贴)添加
{% load content_extras %}
在文件的顶部(第 3 行)换线
{% block pagination %}{% pagination cl %}{% endblock %}
与 {% block pagination %}{% custom_pagination cl %}{% endblock %}
3/创建 content_extras.py
下 templatetags
文件夹并写入 custom_pagination
功能from django import template
from django.contrib.admin.templatetags import admin_list
register = template.Library()
@register.inclusion_tag('admin/pagination.html', takes_context=True)
def custom_pagination(context, cl):
pagination = admin_list.pagination(cl)
if 'group_id' in context:
params = (('group_id', context['group_id']),)
pagination['params'] = params
return pagination
4/创建 pagination.html
(与 changelist.html
相同的位置){% load admin_list %}
{% load i18n %}
{% load content_extras %}
<p class="paginator">
{% if pagination_required %}
{% for i in page_range %}
<a href="?p={{ i }}{% for key, value in params %}{% if key != 'p' %}&{{ key }}={{ value }}{% endif %}{% endfor %}">{{ i }}</a>
{% endfor %}
{% endif %}
{{ cl.result_count }} {% if cl.result_count == 1 %}{{ cl.opts.verbose_name }}{% else %}{{ cl.opts.verbose_name_plural }}{% endif %}
{% if show_all_url %}<a href="{{ show_all_url }}" class="showall">{% trans 'Show all' %}</a>{% endif %}
{% if cl.formset and cl.result_count %}<input type="submit" name="_save" class="default" value="{% trans 'Save' %}">{% endif %}
</p>
关于django - 覆盖 django 管理分页以及 url 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62960518/