pythondjangodjango-admindjango-grappelli

Django (grappelli): how add my own css to all the pages or how to extend admin's base.html?


In Django grappelli, how can I add my own css files to all the admin pages? Or is there a way to extend admin's base.html template?


Solution

  • I found the complex solution. First of all, I found a python package which can help you to tell the Django Template Loader that your template has a highest priority in comparison with others.

    1. pip install django-templateloaderwithpriorities
    2. In your django settings file put next code:

      TEMPLATE_LOADERS = ('templateloaderwithpriorities.Loader', ) + TEMPLATE_LOADERS TEMPLATE_LOADER_PRIORITIES = ['project/admin/templates']

    3. In project/static/css/admin/extra_admin.css put your css.
    4. In project/admin/templates/admin/base_site.html (not in base.html!) put the next code:

      {% extends 'admin/base.html' %} {% load staticfiles %} {% block javascripts %} {{ block.super }} <script>alert('OK')</script> {% endblock %} {% block stylesheets %} <link rel="stylesheet" href="{% static 'css/admin/extra_admin.css' %}"> {{ block.super }} {% endblock %}

    5. That's all! When you go to your site admin page, you see the javascript alert "OK", so in my example I've also included a custom js.

    Everything also works fine with Django Grappelli.