How to override and extend basic Django admin templates?

前端 未结 11 1012
长发绾君心
长发绾君心 2020-11-22 14:11

How do I override an admin template (e.g. admin/index.html) while at the same time extending it (see https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-r

11条回答
  •  [愿得一人]
    2020-11-22 14:29

    Update:

    Read the Docs for your version of Django. e.g.

    https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#admin-overriding-templates https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#admin-overriding-templates https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#admin-overriding-templates

    Original answer from 2011:

    I had the same issue about a year and a half ago and I found a nice template loader on djangosnippets.org that makes this easy. It allows you to extend a template in a specific app, giving you the ability to create your own admin/index.html that extends the admin/index.html template from the admin app. Like this:

    {% extends "admin:admin/index.html" %}
    
    {% block sidebar %}
        {{block.super}}
        

    Extra links

    My extra link
    {% endblock %}

    I've given a full example on how to use this template loader in a blog post on my website.

提交回复
热议问题