Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block content on admin template not overridden

I extended django admin site according to Customize Django admin template and official doc

{% extends 'admin/base_site.html' %}
{% load static %}


{% block branding %}
    <div class="head">
        <h1 id="name">Admin Dashboard abc</h1>
    </div>
{% endblock %}

{% block content %}
<h2>Custom Content</h2>
{% endblock %}


{% block nav-global %}
    <img class="brand_img" src="{% static 'images/ic_launcher.png'%}" width="50" height="50" alt="logo logo">
{% endblock %}

"block branding" & "block nav-global" is displaying correctly but "block content" is not making any change to admin site. The official doc says..

If you want to use the admin layout, extend from admin/base_site.html:

{% extends "admin/base_site.html" %} {% block content %} ... {% endblock %}

And I did what the doc says but is not working. What am I doing wrong?

like image 603
Bigair Avatar asked Feb 27 '26 00:02

Bigair


2 Answers

In order to get {% block content %} to work extending the admin's template, you need to create an index.html file inside the folder admin, located in the template's DIR folder, which is the folder mentioned in Django's documentation, usually templates/admin/index.html, then in this file you can extend the admin/index.html file and replace the block content, for instance:

{% extends "admin/index.html" %}
{% load static %}


{% block content %}
{{ block.super }}
<p>this works</p>
{% endblock %}

To get the template's DIR, you can open up the settings.py file, it's an option like this

TEMPLATES = [
        { 'DIRS': [BASE_DIR / 'templates'], }
]

like image 52
Overclocked Skid Avatar answered Mar 02 '26 15:03

Overclocked Skid


{% block content %} is populated by index.html which extends base_site.html therefore even if you do any changes in base_site.html it will be overriden by index.html's {% block content %}. The possible solution is to delete everything in index.html's block content and call {{block.super}} so if you do any changes in base_site.html they will be passed to index.html.

like image 31
rollingthedice Avatar answered Mar 02 '26 16:03

rollingthedice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!