6

I know this is asked and answered several times but I basically went over all the post on stack overflow and still couldn't get this to work. Right now I am just trying simply change the admin site title. I have the following:

#base_site.html
{% extends "admin/base_site.html" %}

{% block title %}{{ title }} | {{ site_title|default:_('NEW TITLE') }}{% endblock %}

{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('NEW TITLE') }}</a></h1>
{% endblock %}

{% block nav-global %}{% endblock %}

And I tried to put this in

my_site/templates/admin/base_site.html,

my_site/templates/admin/my_app/base_site.html, and

my_site/my_app/templates/admin/base_site.html,

but none of these work.

settings.py:
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
        },
    },
]

I also tried just directly changing django\contrib\admin\templates\admin\base_site.html but still nothing happens.

I am really frustrated now and definitely could use some help, thanks

Updates: Actually I found out that the local template does have effect. enter image description here

Like here, the topmost white bar displays "#base_site.html!!@#" which is what I put in my_site/templates/admin/base_site.html as a comment by chance. So it kinda working, but I still don't understand why I can't change the site title.

Community
  • 1
  • 1
Hansong Li
  • 417
  • 2
  • 11
  • 26
  • If it's not working when you edit the source template, then something seriously weird is going on. Are you sure you're running the server from where you think you are? – Daniel Roseman Aug 24 '16 at 20:57
  • @DanielRoseman yeah I am quite sure about that. So I am really scratching my head for this now – Hansong Li Aug 24 '16 at 21:10
  • https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#customizing-adminsite – allcaps Aug 24 '16 at 22:45
  • This seems to be related to: https://stackoverflow.com/questions/4938491/django-admin-change-header-django-administration-text Need to set a new value to site_header and site_title fields. – Rodolfo Ortega Mar 01 '20 at 00:54
  • I'm facing the same issue now. Any idea how to bring the user tools "welcome" "logout" etc in a custom template? – surya raj Aug 07 '21 at 14:38

5 Answers5

12

Add your Django app above 'django.contrib.admin' in settings -> INSTALLED_APPS. The order of apps in INSTALLED_APPS matters.

# settings.py

...
INSTALLED_APPS = [
    "your_app.yourAppConfig", # add your app here
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
...

use my_site/my_app/templates/admin/base_site.html

put your app where you define this template before 'django.contrib.admin', in INSTALLED_APPS

source link

Azhar Khan
  • 3,829
  • 11
  • 26
  • 32
Mohit Rustagi
  • 198
  • 1
  • 10
  • Thanks but I am pretty sure I did all those already, still in vain – Hansong Li Aug 24 '16 at 21:10
  • 1
    I did not now that the loading order is crucial in the INSTALLED_APPS. I put my app above the 3. party package, now it is using my custom template, thx! – jturi Jul 31 '18 at 10:36
  • This was my problem, and hard to find. – monkut Aug 02 '19 at 02:33
  • @jturi That fixed my problem, thank you! If the templates folder that you have base_site.html in is inside of an app, then that app must be included before django.contrib.admin. – kloddant Oct 01 '19 at 20:09
10

First open your settings.py and add a template folder:

TEMPLATES = [
{
    'DIRS': [
        '/path/to/your/django-project/templates', # Absolute Path
        # os.path.join(BASE_DIR, 'templates') # Relative Path
    ],
    ... 

Then move the file base_site.html to the following directory. It's important to keep the structure on the file system.

/path/to/your/django-project/templates/admin/base_site.html

Content of base_site.html:

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

{% block title %}{{ title }} | {{ site_title|default:_('NEW TITLE') }}{% endblock %}

{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">Your new Title</a></h1>
{% endblock %}

{% block nav-global %}{% endblock %}

Django Documentation - Overriding Admin Site

Hope that helps.

Tobias Ernst
  • 4,214
  • 1
  • 32
  • 30
3

So you should delete including the curly braces, and just write the text you want.

Remove: `{{ site_title|default:_('NEW TITLE') }}`  Write: NEW TITLE
Remove: `{{ site_header|default:_('NEW TITLE' }}`  Write: NEW TITLE

Also be sure that you placed the folders (templates/admin) in the root directory of your project. root directory is the container for all your app folders and also the manage.py file.

In other words, "templates" folder you want to create should be at the same level with manage.py file.

Your folder structure should look like this:

mysite/
    templates/
        admin/
            base_site.html
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py
    ...
    ...
1

I am not sure if I understand right. but you can easy to change the title by doing this:

in the "urls.py" file

from django.contrib import admin

urlpatterns = [
    path('admin/', admin.site.urls),
    .......
]

admin.site.site_header = 'My New Title'
admin.site.site_title = 'My New Title'

Then it should get the job done for you.

Ken
  • 1,234
  • 10
  • 16
  • I like to put `admin.site.site_header = 'My New Title'` and `admin.site.site_title = 'My New Title'` in admin.py of Django app – azmirfakkri May 23 '20 at 15:13
0

To change Django Admin site title, you can add these two lines in the admin.py of your Django app:

from django.contrib import admin

# change Django admin title
admin.site.site_title = 'My Blog Admin'

# change Django admin site header
admin.site.site_header = 'My Blog Admin'
azmirfakkri
  • 571
  • 1
  • 6
  • 18