如何正确扩展django admin / base.html模板? [英] How do I correctly extend the django admin/base.html template?

查看:456
本文介绍了如何正确扩展django admin / base.html模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎应该很简单,但我一定要做错事。我之前已经扩展了个人应用的管理模板,但这是我第一次尝试扩展以全面修改某些内容。



我想更改颜色的整个管理员的帮助文本,所以我想扩展base.html模板的extrastyle块。



所以在我的主模板文件夹中,我创建了admin / base .html中包含此代码:

  {%extends'admin / base.html'%} 

{%block extrastyle%}
{#在整个管理员中更改帮助文本的颜色#}
< style>
.help,p.help {
font-size:10px!important;
颜色:#f00;
}
< / style>
{%endblock%}

现在,当我尝试访问管理员时,服务器完全崩溃,出现bus 10错误。我相信这是因为它试图延伸自己。由于Django首先出现在我的应用模板文件夹中,{%extend'admin / base.html'%}首先发现自己,世界就会爆炸。



但是,如果我尝试将基础HTML放在其他地方不起作用。如果我把它放在我的一个应用程序中,它只适用于该应用程序,但是如果我把它放在任何其他地方,那么它就被忽略了。



从我的理解,这是一个最佳实践扩展而不是覆盖django模板,所以我想让这个工作。但是,如果我能做的唯一方法是重写它,那就是我要采取的路线。

解决方案

你的问题是一个无限的递归循环,因为base.html自己扩展。



为了实现你想要的,你应该替换 admin / base_site.html (依次扩展 base.html)。这样,您只能替换您感兴趣的区块。


This seems like it should be simple but I must be doing something wrong. I've extended admin templates for individual apps before, but this is the first time I've tried extending to modify something across the board.

I want to change the color of the help text across the entire admin, so I want to extend the extrastyle block of the base.html template.

So in my main templates folder I created admin/base.html with this code in it:

{% extends 'admin/base.html' %}

{% block extrastyle %}
    {# Changing the color of the help text across the entire admin #}
    <style>
        .help, p.help {
            font-size: 10px !important;
            color: #f00;
        }
    </style>
{% endblock %}

Now, when I try and access the admin, the server completely crashes with a 'bus 10' error. I believe this is because it is trying to extend itself. Since Django looks first in my app template folders, {% extend 'admin/base.html' %} finds itself first and the world explodes.

However, if I try placing the base html anywhere else it doesn't work. If I place it in one of my apps it works only for that app, but if I place it anywhere else it is just ignored.

From my understanding it's a best practice to extend instead of override django templates, so I would like to get this working. However if the only way I can do it is by overriding it, then that's the route I'll take.

解决方案

Indeed, your problem is an infinite recursion loop as base.html extends itself.

To achieve what you want you should override admin/base_site.html instead (which in turn extends base.html). That way you can replace only the blocks you're interested in.

这篇关于如何正确扩展django admin / base.html模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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