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

查看:33
本文介绍了如何正确扩展 django admin/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.

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

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.

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

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 %}

现在,当我尝试访问管理员时,服务器因总线 10"错误而完全崩溃.我相信这是因为它试图扩展自己.由于 Django 在我的应用模板文件夹中首先查找,因此 {% extend 'admin/base.html' %} 会首先发现自己,然后整个世界都爆炸了.

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.

但是,如果我尝试将基本 html 放在其他任何地方,它就不起作用.如果我把它放在我的一个应用程序中,它只适用于那个应用程序,但如果我把它放在其他任何地方,它就会被忽略.

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.

根据我的理解,扩展而不是覆盖 django 模板是最佳实践,所以我想让它工作.但是,如果我能做到的唯一方法是覆盖它,那么这就是我要走的路线.

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.

推荐答案

确实,您的问题是随着 base.html 扩展自身而导致的无限递归循环.

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

要实现您想要的效果,您应该覆盖 admin/base_site.html(反过来扩展 base.html).这样你就可以只替换你感兴趣的块.

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天全站免登陆