James
James

Reputation: 2542

Django Extend admin index

I wish to make some modifications to the Django admin interface (specifically, remove the "change" link, while leaving the Model name as a link to the page for changes to the instances). I can achieve this by copying and pasting index.html from the admin application, and making the modifications to the template, but I would prefer to only override the offending section by extending the template - however I am unsure how to achieve this as the templates have the same name. I am also open to alternative methods of achieving this effect. (django 1.7, python 3.4.1)

Upvotes: 4

Views: 5876

Answers (5)

Oleksii Dubniak
Oleksii Dubniak

Reputation: 237

In case you want to append new stuff to right sidebar, I'd suggest to use jQuery

{% extends "admin/index.html" %}
{% block extrahead %}
  <script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.min.js"></script>
  <script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
{% endblock %}
{% block sidebar %}
  {{ block.super }}
  <div id="extra_sidebar">
    <div class="module">
      <h2>Custom functions</h2>
      <a style="padding-left: 15px" href="/admin/extra/">My extra link</a>
    </div>
  </div>
  <script>
    (function ($) {
      $(document).ready(function ($) {
        $('#content-related').prepend($('#extra_sidebar').contents());
      });
    })(django.jQuery);
  </script>
{% endblock %}

Upvotes: -1

monkut
monkut

Reputation: 43840

See this answer for overriding standard admin templates (not linked to a specific model)

You need to have the app in which you are overriding the template set BEFORE, 'django.contrib.admin' in your settings.INSTALLED_APPS.

https://stackoverflow.com/a/39964906/24718

Upvotes: 1

James
James

Reputation: 2542

Worked it out - I set admin.site.index_template = "my_index.html" in admin.py, and then the my_index template can inherit from admin/index.html without a name clash.

Upvotes: 8

tr00st
tr00st

Reputation: 339

Might be cleaner to override the index_template for AdminSite:

from django.contrib.admin.sites import AdminSite
AdminSite.index_template = '...'

Though again, that might be made more external-code-friendly by either: changing this on a custom instance before binding, or subclassing a custom AdminSite and overriding there, and registering that custom AdminSite instead.

Relevant documentation:

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#root-and-login-templates

Edit: To clarify - this would allow you to just override the section you're changing in the template, and so inherit any upstream changes.

Upvotes: 3

Brandon Taylor
Brandon Taylor

Reputation: 34553

If you want to remove change permissions for a model, you can do this programmatically in your admin class for the model rather than modifying the templates. This gives you the advantage of being able to enable/disable the link based on user criteria. Changing the permission will be reflected at the Change List, Change Form and in the admin index.

from django.contrib import admin
from your_app.models import YourModel

class CustomModelAdmin(admin.ModelAdmin):
    def has_change_permission(self, obj=None):
        # check request.user creds, etc
        return False

admin.site.register(YourModel, CustomModelAdmin)

See: https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.ModelAdmin.has_change_permission for more information about Django admin options

Upvotes: 0

Related Questions