Django Admin:为两个管理站点使用不同的模板 [英] Django Admin: using different templates for two admin site

查看:139
本文介绍了Django Admin:为两个管理站点使用不同的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



理想情况下,我想要:

 #对于第一个管理站点
myproject / templates / admin-a /
base.html
base_site.html

 #第二个管理网站
myproject / templates / admin-b /
base.html
base_site.html

任何想法?



谢谢,
Stéphane

解决方案

第一个选项将是两个 ModelAdmin 类,一个派生自第二个,另外一些额外的参数定义模板,这里是管理代码的一部分:

 #自定义模板(设计为子类覆盖)
add_form_template =无
change_form_template =无
change_list_template =无
delete_confirma tion_template =无
delete_selected_confirmation_template =无
object_history_template =无

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



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



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

  TEMPLATE_DIRS =('templates-a',)

第二个

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

此处提供了两个模板目录都可以为您提供回退选项,因此您将定义只有那些不同的模板。



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


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

Any ideas ?

Thanks, Stéphane

解决方案

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

这篇关于Django Admin:为两个管理站点使用不同的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆