关于python:修改Django管理模板

modifying django admin template

我正在使用Django Framework在python中编写应用程序,并且试图找出是否有可能针对特定模块修改现有的django admin应用程序模板(change_form.html,base_site.html ...)。 在文件夹项目中,我已经将template / admin目录放在修改了admin应用程序模板的位置,但是对于一个特定的模块,我想使用其他模板。 特定模块在单独的应用程序中。

如果您能让我知道如何实现这一目标,或者至少将我指向可以找到它的文档,将不胜感激。

提前非常感谢您。


您可以通过在所需模板自定义其级别上覆盖特定模板来完成此操作。 例如,如果您想为应用程序修改change_form.html,则可以在templates / admin / yourapp / change_form.html中创建(或从contrib / admin / templates ...复制change_form.html)模板。 对于应用程序中的模型,您可以将其放置在templates / admin / yourapp / yourmodel / change_form.html中。 您可以在这里找到更多信息

从上面的链接:

For example, if we wanted to add a tool to the change list view for all the models in an app named my_app, we would copy contrib/admin/templates/admin/change_list.html to the templates/admin/my_app/ directory of our project, and make any necessary changes.

If we wanted to add a tool to the change list view for only a specific model named ‘Page’, we would copy that same file to the templates/admin/my_app/page directory of our project.


您可以在ModelAdmin级别覆盖,而不仅仅是通过template /名称空间覆盖模板。

编辑:我刚刚注意到您正在寻找每个模块可能具有多个版本的非对象特定模板页面(至少base_site.html)。 我不确定您是否可以在不自定义AdminSite的情况下做到这一点。 您可以另外注册多个管理站点,尽管那里的用处取决于您的具体情况。