Django:覆盖“不可覆盖” 每个应用程序而不是每个项目的管理模板?
Django 文档明确指出了以下内容:
并非 contrib\admin\templates\admin 中的每个模板都可以按应用程序或模型覆盖。
然后它列出了可以的,以及 base.html
、base_site.html
和 index.html
– 我感兴趣的 –不在所列之列。 它们可以按项目覆盖,但不能按应用程序覆盖。
我的问题是:有没有一种方法可以解决这个问题,而不涉及编辑 django.contrib.admin 内的代码? 我愿意考虑一些猴子补丁解决方案:-)。 我真的希望我的应用程序在其 templates
目录中拥有这三个文件的自定义版本,并且让使用该应用程序的每个项目都使用这些文件。
我感兴趣的原因是,我正在创建一个大型、可重用的应用程序,具有高度定制的管理界面,并且每个项目覆盖“核心”模板并不是最好的解决方案,因为我必须复制将自定义模板添加到应用程序使用的每个项目的模板目录中。发布对这些核心模板进行新修改的应用程序的新版本意味着将所有内容重新复制到受影响的项目。 啊。
我理解决定仅使每个应用程序可覆盖选定的几个模板背后的原因; 毕竟,如果可以覆盖所有这些,那么哪个应用程序的覆盖管理员会优先?
但就我而言,该应用程序将成为多个客户项目的“核心”,而这些项目中的其他应用程序仅充当配角。
现有模板的基于 CSS 的自定义只能让您到目前为止,除非绝对必要,否则我不愿意依赖 JavaScript DOM 操作解决方案。
我想到的一种解决方案是将自定义 base.html
等模板放入 appname/templates/admin/ 中,然后将它们符号链接到项目的模板文件夹。 这样,应用程序的任何更新都会自动在项目级别生效。
如果没有更好的建议,符号链接可能是我选择的方法,但我想听听是否有人有更好的解决方案。
The Django documentation states the following clearly:
Not every template in contrib\admin\templates\admin may be overridden per app or per model.
It then lists the ones that can, and base.html
, base_site.html
and index.html
– the ones I'm interested in – are not among those listed. They can be overridden per-project, but not per-app.
My question is: is there a way around this that doesn't involve editing the code inside django.contrib.admin? I'm willing to consider some monkeypatching solutions :-). I really want my app to have custom versions of those three files inside its templates
directory, and have each project that uses the app to use those.
The reason I'm interested is that I'm creating a large, reusable app with a heavily customized admin interface, and per-project overrides of the "core" templates aren't the best solution, since I'd have to copy the custom templates to the template directory of each project that the app gets used in. Releasing new versions of the app with new modifications to those core templates would mean re-copying everything to the affected projects. Ugh.
I understand the reasoning behind the decision to only make a select few templates overridable per-app; after all, if overriding them all was possible, which app's overridden admin would take precedence?
But in my case, the app will be the "centerpiece" of several client projects, with other apps in those projects merely being in a supporting role.
CSS-based customization of the existing templates only gets you so far, and I'm hesitant to rely on JavaScript DOM manipulation solutions unless absolutely necessary.
One solution that comes to mind is to place the custom base.html
etc. templates inside appname/templates/admin/ and then symlink them to the project's templates folder. That way any updates to the app will automatically take effect on the project level.
Symlinking is probably my method of choice if nothing better is suggested, but I'd like to hear if anyone has a nicer solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我所看到的,您的目标是覆盖整个项目的模板,而不是应用程序或模型的模板,但您不想将模板放在项目的模板文件夹中。
因此,您应该在“your_app/templates/admin”文件夹中创建“base.html”等。
接下来,您必须告诉 django,模板不仅应该从项目的模板文件夹加载,还应该从应用程序的文件夹加载。
这可以使用 settings.py 文件中的 TEMPLATES_DIR 变量来完成。 像那样:
As I see your goal is to override templates for entire project, not for app or for model, but you don't want to put templates in project's template folder.
So you should just create 'base.html', etc. in 'your_app/templates/admin' folder.
Next you have to tell django that templates should be loaded not only from project's template folder, but also from your app's folder.
This can be done using
TEMPLATES_DIR
variable in settings.py file, smth. like that: