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

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

问题描述

关于扩展django管理模板的一个(不是这样)快速的问题。



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



我搜索了代码,但没有找到一种方法来做到这一点。任何指针都非常感激。代码也将帮助。



PS:我知道我应该设计自己的界面,但这是一个内部项目,我没有那么多时间。另外,django界面是非常好的。



提前谢谢。

解决方案>

要扩大Yuji的答案,以下是覆盖 change_list_results.html ...




的详细信息

如步骤1所述覆盖 changelist_view ,并描述这里在djangoproject 通过放置在上述步骤2中相应的目录中进行自动覆盖。 (请注意,上面显示的步骤2路径是特定于模型的,App-specific将在TEMPLATE_DIRS中定义的任何目录下为 /admin/<MyAppName>/change_list.html 元组。)



(也许更容易)只需指定 ModelAdmin.change_list_template 此处所述与任何可发现的模板文件名。 (尽管如果您保留名称 change_list.html ,请确保不要直接存入 / admin 文件夹中,否则扩展标签将导致递归。)

  class MyModelAdmin admin.ModelAdmin):
change_list_template ='change_list.html'#绝对不是admin / change_list.html'
#...






在您的 change_list.html 模板中, p>

  {%extendsadmin / 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 的目录),其中包含与上面的加载标签相对应的文件

 #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 以上)使用您添加的功能。



请注意,这些步骤不包括额外的上下文模板,但可以轻松扩展。 (我这样做的原因是为CSS添加课程,并为没有按结果列表排序的领先< tbody> 添加课程。)






其他



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

 #MyAppName / templatetags / myapptags.py 

from django import template
从django.contrib.admin.templatetags.admin_list导入result_list作为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 返回可以包含在您的结果模板中(例如 {{myextraco ntext}}


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

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: 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.

Thank you in advance.

解决方案

To expand on Yuji's answer, here are some specifics on overriding change_list_results.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.)

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'
    # ...


In your change_list.html template, have at a minimum

{% 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 %}


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)


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

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.)


ADDITIONAL:

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)

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天全站免登陆