gpt4 book ai didi

django - 覆盖 django 管理分页以及 url 参数

转载 作者:行者123 更新时间:2023-12-04 13:34:32 26 4
gpt4 key购买 nike

我想为我的管理面板实现自定义分页。
我的网址如下所示: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_viewadmin.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.pytemplatetags文件夹并写入 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com