扩展 Django 管理模板 - 更改更改列表
关于扩展 django 管理模板的(不是那么)快速问题。
我试图通过在包含与该对象相关的一些对象的结果行(row1 和 row2 类)之间添加中间行来更改特定模型的结果列表(django 行话中的更改列表)。
我搜索了代码,但没有找到实现此目的的方法。非常感谢任何指点。代码也会有帮助。
PS:我知道我应该设计自己的界面,但这是一个内部项目,我没有那么多空闲时间。另外,django 的界面也非常好。
先感谢您。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了扩展 Yuji 的答案,这里有一些关于覆盖
change_list_results.html
的细节...覆盖
changelist_view
如上面步骤 1 中所述,并且还描述了 在 djangoproject。 或者通过将其放置在适当的目录中(如上面的步骤 2 所示)来自动覆盖。 (请注意,上面显示的第 2 步路径是特定于模型的。特定于应用程序的路径是 TEMPLATE_DIRS 元组中定义的任何目录下的/admin//change_list.html
。) 或者(也许更容易)只需指定
ModelAdmin.change_list_template
,如此处 以及任何可发现的模板文件名。 (不过,如果您保留名称change_list.html
,请确保不要直接存入/admin
文件夹,否则extends
标记将导致递归。)在您的
change_list.html
模板中,至少创建一个
//templatetags
包(包含__init__.py 的目录
)以及与上面的 load 标记相对应的文件复制并编辑 Django 的
change_list_results.html
(如上面的my_change_list_results.html
)以使用您添加的功能。请注意,这些步骤不包括模板的额外上下文,但可以轻松扩展。 (我这样做的原因是添加 CSS 类和未与结果列表一起排序的前导
。)
附加:
要包含额外的上下文,更改您的 templatetags 模块,如下所示:
然后,
myextracontext
的值或任何foo_extra
返回的值都可以包含在您的结果模板中(例如{{ myextracontext }}< /代码>)
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 namechange_list.html
, be sure not to deposit directly into the/admin
folder, else theextends
tag will cause a recursion.)In your
change_list.html
template, have at a minimumCreate a
/<MyAppName>/templatetags
package (a directory containing__init__.py
) with a file corresponding to the load tag aboveCopy 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:
Then, the value of
myextracontext
or whateverfoo_extra
returns can be included in your results template (as e.g.{{ myextracontext }}
)第 1 步:覆盖更改列表视图:
您必须覆盖模板,而不是像使用
add_view /change_view
那样指定模板。首先,覆盖
def changelist_view(self, request, extra_context=None):
在您的ModelAdmin
中。请记住调用 super(foo, self).changelist_view(request, extra_context) 并返回它。第 2 步:覆盖模板:
接下来,覆盖
templates/admin/my_app/my_model/change_list.html
中特定于应用程序的变更列表模板(或者不覆盖。如果您愿意,也可以使用全局变更列表覆盖)。第 3 步:复制结果列表功能
我认为您可以复制
result_list
功能(定义新的模板标记),也可以通过将 result_list 函数和模板复制并粘贴到您的视图中来伪造它。您可以看到管理员使用此
admin/change_list_results.html
模板来呈现各个列,因此您需要使用其中一种方法来替换此模板标记。由于它正在寻找全局模板,因此我不会覆盖它。
可以专门为您的视图定义一个带有新模板的新标签,或者直接将
result_list(cl)
发送到您的模板并采用admin/change_list_results.html
直接在您的change_list.html
模板。Step 1: Overriding changelist view:
You'll have to override a template as opposed to specifying one like you can with
add_view / change_view
.First things first, override
def changelist_view(self, request, extra_context=None):
in yourModelAdmin
. Remember to callsuper(foo, self).changelist_view(request, extra_context)
and to return that.Step 2: Overriding templates:
Next, override the app-specific changelist template at
templates/admin/my_app/my_model/change_list.html
(or not.. you can use a global changelist override too if you'd like).Step 3: Copy result list functionality
I think you can either copy
result_list
functionality (define a new template tag) or fake it by copying and pasting the result_list function and template include into your view.You can see the admin uses this
admin/change_list_results.html
template to render individual columns so you'll need to use one of the methods to replace this template tag.Since it's looking for a global template, I wouldn't override it.
Either define a new tag w/ new template specifically for your view, or send
result_list(cl)
to your template directly and adopt theadmin/change_list_results.html
for use directly in yourchange_list.html
template.如果您想要完全覆盖
admin_list
模板标记,请参阅我在另一个线程中的帖子。 https://stackoverflow.com/a/55597294/11335685我知道它与
change_list 没有直接关系
模板,但其逻辑位于admin_list
模板标记内的函数。这种方法的缺点是我没有找到一种只覆盖特定功能并使用其他功能的方法。
但这仍然是唯一允许我不弄乱 html 模板而只改变
result_list
函数行为的方法。希望对任何人都有帮助。
If you want a complete override of the
admin_list
template tag, here is my post in another thread. https://stackoverflow.com/a/55597294/11335685I know it is not directly related to the
change_list
template, but the logic for it lays on the functions inside theadmin_list
template tag.A disadvantage of that approach is i didn't find a way of overriding only specific function and get use of the other ones.
Still that was the only approach that was allowing me to not mess with the html templates and only alter the behavior of
result_list
function.Hope that helps anyone.