且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

扩展 Django 管理模板 - 更改更改列表

更新时间:2023-09-18 21:49:04

为了扩展 Yuji 的答案,这里有一些关于覆盖 change_list_results.html ...

To expand on Yuji's answer, here are some specifics on overriding change_list_results.html ...

覆盖 changelist_view 如上文步骤 1 中所述,并且还描述了 这里在 djangoproject.通过放置在上述步骤 2 中的相应目录中来自动覆盖.(请注意,上面显示的第 2 步路径是特定于模型的.特定于应用程序的将是 TEMPLATE_DIRS 元组中定义的任何目录下的 /admin//change_list.html.)

Override changelist_view as described above in step 1, and also described here at djangoproject. Or auto-override by placing in the appropriate directory as in step 2 above. (Note that the step 2 path shown above is model-specific. App-specific would be /admin/<MyAppName>/change_list.html under any directory defined in the TEMPLATE_DIRS tuple.)

或者(可能更简单)只需按照说明指定 ModelAdmin.change_list_template 这里 带有任何可发现的模板文件名.(虽然,如果您保留名称 change_list.html,请务必不要直接存入 /admin 文件夹,否则 extends 标签会导致递归.)

Or (perhaps easier) simply specify ModelAdmin.change_list_template as explained here with any discoverable template filename. (Although, if you retain the name change_list.html, be sure not to deposit directly into the /admin folder, else the extends tag will cause a recursion.)

class MyModelAdmin(admin.ModelAdmin):
    change_list_template = 'change_list.html' # definitely not 'admin/change_list.html'
    # ...

在您的 change_list.html 模板中,至少有

{% extends "admin/change_list.html" %}
{% load i18n admin_static admin_list %}
{% load myapptags %}

{% block result_list %}
  {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}
  {% result_list cl %}
  {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}
{% endblock %}

创建一个/<MyAppName>/templatetags包(包含__init__.py的目录),文件对应上面的load标签


Create a /<MyAppName>/templatetags package (a directory containing __init__.py) with a file corresponding to the load tag above

# MyAppName/templatetags/myapptags.py

from django import template
from django.contrib.admin.templatetags.admin_list import result_list

register = template.Library()
register.inclusion_tag('my_change_list_results.html')(result_list)

复制和编辑 Django 的 change_list_results.html(例如上面的 my_change_list_results.html)以使用您添加的功能.


Copy and edit Django's change_list_results.html (as e.g. my_change_list_results.html above) to use your added functionality.

请注意,这些步骤不包括模板的额外上下文,但可以轻松扩展.(我这样做的原因是为 CSS 添加类和一个未按结果列表排序的前导 <tbody>.)

Note that these steps do not include extra context for the template, but can easily be expanded as such. (My reason for doing this was to add classes for CSS and a leading <tbody> that was not sorted with the results list.)

附加:

要包含额外的上下文,请按如下方式更改您的模板标签模块:

To include extra context, change your templatetags module as follows:

# MyAppName/templatetags/myapptags.py

from django import template
from django.contrib.admin.templatetags.admin_list import result_list as admin_list_result_list

def result_list(cl):
    mycl = {'myextracontext': 'something extra'}
    mycl.update(foo_extra())
    mycl.update(admin_list_result_list(cl))
    return mycl

register = template.Library()
register.inclusion_tag('my_change_list_results.html')(result_list)

然后,myextracontext 的值或任何 foo_extra 返回的值都可以包含在您的结果模板中(例如 {{ myextracontext }})

Then, the value of myextracontext or whatever foo_extra returns can be included in your results template (as e.g. {{ myextracontext }})