扩展 Django 管理模板 - 更改更改列表 [英] Extending Django Admin Templates - altering change list

查看:23
本文介绍了扩展 Django 管理模板 - 更改更改列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于扩展 django 管理模板的一个(不是那么)快速的问题.

A (not so) quick question about extending django admin templates.

我正在尝试通过在包含与该对象相关的一些对象的结果行(row1 和 row2 类)之间添加一个中间行来更改特定模型的结果列表(django lingo 中的更改列表).

I'm trying to change the result list (change list in django lingo) of a specific model by adding an intermediary row between the result rows (row1 and row2 classes) that contains some objects related to that object.

我搜索了代码,但没有找到执行此操作的方法.非常感谢任何指针.代码也会有所帮助.

I searched the code but haven't found a way to do this. Any pointers are very much appreciated. Code will also help too.

PS:我知道我应该设计自己的界面,但这是一个内部项目,我没有那么多空闲时间.另外,django 的界面真的很不错.

PS: I know I should be designing my own interface, but this is an internal project and I don't have that much time to spare. Also, the django interface is really nice.

提前谢谢你.

推荐答案

为了扩展 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 }})

这篇关于扩展 Django 管理模板 - 更改更改列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆