Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I correctly extend the django admin/base.html template?

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.

like image 802
Eric Ressler Avatar asked Jul 31 '13 18:07

Eric Ressler


People also ask

How do you extend HTML?

By extending the native <a> tag, we can simply add an is attribute to indicate it is a client-side link, so it won't make the browser go to the page specified in its href attribute when clicked.

How do I override Django admin base in HTML?

To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory. Let's say we want to change base. html – to do so, we would copy that file and paste it into the templates/admin directory in our project.

Can we customize Django admin template?

The Django admin is a powerful built-in tool giving you the ability to create, update, and delete objects in your database using a web interface. You can customize the Django admin to do almost anything you want.

How do I use extends in Django?

Using the extends tag in Django requires several things. (1) First, you need a Django template to extend. This is the template whose base code you want to use for other templates. (2) Next, you need to add the Django extend block content tags where each of the other templates will be loaded in.


1 Answers

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.

like image 190
ppetrid Avatar answered Dec 21 '22 04:12

ppetrid