Django Admin - change header 'Django administration' text

后端 未结 18 2239
眼角桃花
眼角桃花 2020-11-28 01:14

How does one change the \'Django administration\' text in the django admin header?

It doesn\'t seem to be covered in the \"Customizing the admin\" documentation.

相关标签:
18条回答
  • 2020-11-28 01:24

    Update: If you are using Django 1.7+, see the answer below.


    Original answer from 2011: You need to create your own admin base_site.html template to do this. The easiest way is to create the file:

    /<projectdir>/templates/admin/base_site.html
    

    This should be a copy of the original base_site.html, except putting in your custom title:

    {% block branding %}
    <h1 id="site-name">{% trans 'my cool admin console' %}</h1>
    {% endblock %}
    

    For this to work, you need to have the correct settings for your project, namely in settings.py:

    • Make sure /projectdir/templates/ is added into TEMPLATE_DIRS.
    • Make sure django.template.loaders.filesystem.Loader is added into TEMPLATE_LOADERS.

    See docs for more information on settings.py.

    0 讨论(0)
  • 2020-11-28 01:24

    you do not need to change any template for this work you just need to update the settings.py of your project. Go to the bottom of the settings.py and define this.

    admin.site.site_header = 'My Site Admin'
    

    In this way you would be able to change the header of the of the Django admin. Moreover you can read more about Django Admin customization and settings on the following link.

    Django Admin Documentation

    0 讨论(0)
  • 2020-11-28 01:25

    First of all, you should add templates/admin/base_site.html to your project. This file can safely be overwritten since it’s a file that the Django devs have intended for the exact purpose of customizing your admin site a bit. Here’s an example of what to put in the file:

    {% extends "admin/base.html" %}
    {% load i18n %}
    
    {% block title %}{{ title }} | {% trans 'Some Organisation' %}{% endblock %}
    
    {% block branding %}
    <style type="text/css">
      #header
      {
        /* your style here */
      }
    </style>
    <h1 id="site-name">{% trans 'Organisation Website' %}</h1>
    {% endblock %}
    
    {% block nav-global %}{% endblock %}
    

    This is common practice. But I noticed after this that I was still left with an annoying “Site Administration” on the main admin index page. And this string was not inside any of the templates, but rather set inside the admin view. Luckily it’s quite easy to change. Assuming your language is set to English, run the following commands from your project directory:

    $ mkdir locale
    $ ./manage.py makemessages -l en
    

    Now open up the file locale/en/LC_MESSAGES/django.po and add two lines after the header information (the last two lines of this example)

    "Project-Id-Version: PACKAGE VERSION\n"
    "Report-Msgid-Bugs-To: \n"
    "POT-Creation-Date: 2010-04-03 03:25+0200\n"
    "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
    "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
    "Language-Team: LANGUAGE <LL@li.org>\n"
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    
    msgid "Site administration"
    msgstr "Main administration index"
    

    After this, remember to run the following command and reload your project’s server:

    $ ./manage.py compilemessages
    

    source: http://overtag.dk/wordpress/2010/04/changing-the-django-admin-site-title/

    0 讨论(0)
  • 2020-11-28 01:27

    Since I only use admin interface in my app, I put this in the admin.py :

    admin.site.site_header = 'My administration'
    
    0 讨论(0)
  • 2020-11-28 01:31

    For Django 2.1.1 add following lines to urls.py

    from django.contrib import admin
    
    # Admin Site Config
    admin.sites.AdminSite.site_header = 'My site admin header'
    admin.sites.AdminSite.site_title = 'My site admin title'
    admin.sites.AdminSite.index_title = 'My site admin index'
    
    0 讨论(0)
  • 2020-11-28 01:32

    A simple complete solution in Django 1.8.3 based on answers in this question.

    In settings.py add:

    ADMIN_SITE_HEADER = "My shiny new administration"
    

    In urls.py add:

    from django.conf import settings
    admin.site.site_header = settings.ADMIN_SITE_HEADER
    
    0 讨论(0)
提交回复
热议问题