Django Admin:为两个管理站点使用不同的模板

发布于 2024-10-26 02:22:00 字数 554 浏览 4 评论 0原文

我有一个具有两个不同管理站点的 Django 项目(如 文档

我想为每个人都有不同的自定义模板。 我知道如何通过将 html 文件放入 myproject/templates/admin/ 目录来覆盖自定义模板。 但是,两个管理站点都使用这些模板!

我不明白如何指定另一组自定义模板。

理想情况下,我希望有:

# For first admin site
myproject/templates/admin-a/
   base.html
   base_site.html

和:

# For second admin site
myproject/templates/admin-b/
   base.html
   base_site.html

I've a Django project with two different admin-site (as described in documentation )

I would like to have different custom template for each of them.
I know how to override custom template, by putting html files in myproject/templates/admin/ directory.
However, both admin-site use those templates !

I don't understand how to specify another set of custom templates.

Ideally, I would like having:

# For first admin site
myproject/templates/admin-a/
   base.html
   base_site.html

and:

# For second admin site
myproject/templates/admin-b/
   base.html
   base_site.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2024-11-02 02:22:00

第一个选项是有两个 ModelAdmin 类,一个派生自第二个类,并带有一些定义模板的附加参数,这是管理代码的一部分:

# Custom templates (designed to be over-ridden in subclasses)
add_form_template = None
change_form_template = None
change_list_template = None
delete_confirmation_template = None
delete_selected_confirmation_template = None
object_history_template = None

可以在您的管理类中设置上述变量。

第二种方法是将基本模板名称传递到模板中,然后使用此(变量)作为 extends 模板标记的参数。 此处的文档。

第三个选项是有两个实例代码运行,但有两个配置具有不同的设置变量TEMPLATE_DIRS,第一个例如:

TEMPLATE_DIRS = ('templates-a',)

第二个

TEMPLATE_DIRS = ('template-b', 'template-a')

在这里拥有两个模板目录会给您一个后备选项,因此您将只定义那些不同的模板。

第三个选项最容易实现(无需更改代码),但它需要 2 个独立的实例同时工作(消耗更多的系统资源)。

first option will be to have two ModelAdmin classes, one derived from second one, with some additional parameters defining templates, here is part of the admin code:

# Custom templates (designed to be over-ridden in subclasses)
add_form_template = None
change_form_template = None
change_list_template = None
delete_confirmation_template = None
delete_selected_confirmation_template = None
object_history_template = None

above variables can be set in your admin class.

second way is to pass a base template name into the template, and then use this (variable) as a parameter to the extends template tag. Documentation here.

third option wil be to have a two instances of code running, but with two configs with different setting variable TEMPLATE_DIRS, first one e.g.:

TEMPLATE_DIRS = ('templates-a',)

second

TEMPLATE_DIRS = ('template-b', 'template-a')

Having both template dirs here gives you an fallback option, so you will define only those templates which are different.

Third option is easiest to implement (no change to the code) but it requires 2 separated instances working at the same time (more system resources consumed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文