5

How can a page be linked to the admin page of django? I have tried changing the base.html page but it is getting reflected to all the html pages of the admin. Can this be linked just to the index page of admin where all the app are listed.

Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53
user2207300
  • 121
  • 1
  • 3
  • 5

3 Answers3

5

You have to override Django admin index.html page

First step:

Create new file in your templates folder call it 'index.html' for example. For example file path will be like: 'YOUR_PROJECT/templates/admin/index.html' and copy the following code:

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

{% block content %}
<h1>YOUR EXTRA CODE</h1>
{{ block.super }}
{% endblock %}

Second step:

Create new file in YOUR_MAIN_PROJECT folder -alongside settings.py- call it admin.py and copy the following code:

from django.contrib.admin import AdminSite


class CustomAdmin(AdminSite):
    index_template = 'admin/base_site.html'


custom_admin_site = CustomAdmin(name='admin')

Third step:

In YOUR_APP admin.py file put the following code:

from YOUR_MAIN_PROJECT.admin import custom_admin_site
from .models import YOUR_MODEL_1, YOUR_MODEL_2

custom_admin_site.register(YOUR_MODEL_1)
custom_admin_site.register(YOUR_MODEL_2)

I hope this helps, your respond is appreciated

Omar Hafez
  • 122
  • 1
  • 3
  • 9
  • 2nd and 3rd steps can be omitted if you put your extended template in `PROJECT_ROOT/MYAPP/templates/admin/index.html` and MYAPP is in `INSTALLED_APPS` and you have `"APP_DIRS": True` in the `TEMPLATES` setting in `settings.py` – Anentropic Dec 06 '22 at 17:17
1
<a href="{% url 'admin:index' %}">Admin panel</a>

But you have to have to add admin.site.urls in your URLS.py

url(r'^admin/', include(admin.site.urls)),

But if you are trying to render admin site in one of your sites (let's say index):

url(r'', include(admin.site.urls)),

That should do the trick

sebb
  • 1,956
  • 1
  • 16
  • 28
  • Thank you for your answer. But i don't want to link the admin page to my html page. Insetad, i want to access my html page from the admin. – user2207300 Mar 24 '15 at 05:21
  • 1
    So you have to edit your templates from contrib.admin. If you're using virtualnenv, it should be in `your_env/local/lib/python_version/django/contrib/admin/templates` – sebb Mar 24 '15 at 11:41
  • 4
    @SebastianBurzyński: Instead of modifying the template in the library location, it is better to copy it into your project and modify it. That way, you don't lose it if Django is upgraded or reinstalled. – Leif Arne Storset Mar 31 '15 at 16:36
  • Correct ;) Thought it was obvious ;) – sebb Apr 01 '15 at 06:56
0

Reference : How to override and extend basic Django admin templates?

You can customize the index template as:

# urls.py
...
from django.contrib import admin

admin.site.index_template = 'admin/my_custom_index.html'
admin.autodiscover()

and place your template in /templates/admin/my_custom_index.html

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

{% block content %}
    {{block.super}}
    <div>
        <h1>Others</h1>
        <a href="/v1/my_new_link/">A new link menu</a>
    </div>
{% endblock %}
david euler
  • 714
  • 8
  • 12