How to override and extend basic Django admin templates?

前端 未结 11 1078
长发绾君心
长发绾君心 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:44

    This site had a simple solution that worked with my Django 1.7 configuration.

    FIRST: Make a symlink named admin_src in your project's template/ directory to your installed Django templates. For me on Dreamhost using a virtualenv, my "source" Django admin templates were in:

    ~/virtualenvs/mydomain/lib/python2.7/site-packages/django/contrib/admin/templates/admin
    

    SECOND: Create an admin directory in templates/

    So my project's template/ directory now looked like this:

    /templates/
       admin
       admin_src -> [to django source]
       base.html
       index.html
       sitemap.xml
       etc...
    

    THIRD: In your new template/admin/ directory create a base.html file with this content:

    {% extends "admin_src/base.html" %}
    
    {% block extrahead %}
    
    {% endblock %}
    

    FOURTH: Add your admin favicon-admin.ico into your static root img folder.

    Done. Easy.

提交回复
热议问题