问题:Django Admin-更改标题“ Django管理”文本

如何更改django管理员标头中的“ Django管理”文本?

“自定义管理员”文档中似乎没有涉及它。

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.


回答 0

更新:如果您使用的是Django 1.7+,请参见下面答案


2011年的原始答案: 您需要创建自己的管理base_site.html模板才能执行此操作。最简单的方法是创建文件:

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

这应该是原始base_site.html版本的副本,但要输入您的自定义标题:

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

为此,您需要为项目设置正确的设置,即settings.py

  • 确保/projectdir/templates/已添加到中TEMPLATE_DIRS
  • 确保django.template.loaders.filesystem.Loader已添加到中TEMPLATE_LOADERS

有关更多信息,请参见docssettings.py

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.


回答 1

从Django 1.7开始,您不需要覆盖模板。现在,您可以在自定义AdminSite上实现site_headersite_titleindex_title属性,以便轻松更改管理站点的页面标题和标题文本。创建一个AdminSite子类并将您的实例挂接到URLconf中:

admin.py:

from django.contrib.admin import AdminSite
from django.utils.translation import ugettext_lazy

class MyAdminSite(AdminSite):
    # Text to put at the end of each page's <title>.
    site_title = ugettext_lazy('My site admin')

    # Text to put in each page's <h1> (and above login form).
    site_header = ugettext_lazy('My administration')

    # Text to put at the top of the admin index page.
    index_title = ugettext_lazy('Site administration')

admin_site = MyAdminSite()

urls.py:

from django.conf.urls import patterns, include
from myproject.admin import admin_site

urlpatterns = patterns('',
    (r'^myadmin/', include(admin_site.urls)),
)

更新:如oxfn所指出的,您可以简单地urls.pyadmin.py不设置子类的情况下设置AdminSite

admin.site.site_header = 'My administration'

As of Django 1.7 you don’t need to override templates. You can now implement site_header, site_title, and index_title attributes on a custom AdminSite in order to easily change the admin site’s page title and header text. Create an AdminSite subclass and hook your instance into your URLconf:

admin.py:

from django.contrib.admin import AdminSite
from django.utils.translation import ugettext_lazy

class MyAdminSite(AdminSite):
    # Text to put at the end of each page's <title>.
    site_title = ugettext_lazy('My site admin')

    # Text to put in each page's <h1> (and above login form).
    site_header = ugettext_lazy('My administration')

    # Text to put at the top of the admin index page.
    index_title = ugettext_lazy('Site administration')

admin_site = MyAdminSite()

urls.py:

from django.conf.urls import patterns, include
from myproject.admin import admin_site

urlpatterns = patterns('',
    (r'^myadmin/', include(admin_site.urls)),
)

Update: As pointed out by oxfn you can simply set the in your urls.py or admin.py directly without subclassing AdminSite:

admin.site.site_header = 'My administration'

回答 2

有一个简单的方法来设置管理站点标题- urls.py像这样将其分配给当前管理实例

admin.site.site_header = 'My admin'

或者可以用单独的方法实现一些头构建魔术

admin.site.site_header = get_admin_header()

因此,在简单情况下,无需子类化 AdminSite

There is an easy way to set admin site header – assign it to current admin instance in urls.py like this

admin.site.site_header = 'My admin'

Or one can implement some header-building magic in separate method

admin.site.site_header = get_admin_header()

Thus, in simple cases there’s no need to subclass AdminSite


回答 3

在其中urls.py可以覆盖3个最重要的变量:

from django.contrib import admin

admin.site.site_header = 'My project'                    # default: "Django Administration"
admin.site.index_title = 'Features area'                 # default: "Site administration"
admin.site.site_title = 'HTML title from adminsitration' # default: "Django site admin"

参考:有关这些属性的Django文档

In urls.py you can override the 3 most important variables:

from django.contrib import admin

admin.site.site_header = 'My project'                    # default: "Django Administration"
admin.site.index_title = 'Features area'                 # default: "Site administration"
admin.site.site_title = 'HTML title from adminsitration' # default: "Django site admin"

Reference: Django documentation on these attributes.


回答 4

Django 1.8.3中基于此问题的答案的简单完整解决方案。

settings.py添加:

ADMIN_SITE_HEADER = "My shiny new administration"

urls.py添加:

from django.conf import settings
admin.site.site_header = settings.ADMIN_SITE_HEADER

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

回答 5

最简单的方法是确保您拥有

from django.contrib import admin

然后将它们添加到url.py主应用程序的底部

admin.site.site_title = "Your App Title"
admin.site.site_header = "Your App Admin" 

The easiest way of doing it make sure you have

from django.contrib import admin

and then just add these at bottom of url.py of you main application

admin.site.site_title = "Your App Title"
admin.site.site_header = "Your App Admin" 

回答 6

对于Django 2.1.1,将以下行添加到 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'

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'

回答 7

正如您在模板中看到的那样,文本是通过本地化框架传递的(请注意transtemplate标签的使用)。您可以更改翻译文件以覆盖文本,而无需制作自己的模板副本。

  1. mkdir locale

  2. ./manage.py makemessages

  3. 编辑locale/en/LC_MESSAGES/django.po,添加以下行:

    msgid "Django site admin"
    msgstr "MySite site admin"
    
    msgid "Django administration"
    msgstr "MySite administration"
  4. ./manage.py compilemessages

参见https://docs.djangoproject.com/en/1.3/topics/i18n/localization/#message-files

As you can see in the templates, the text is delivered via the localization framework (note the use of the trans template tag). You can make changes to the translation files to override the text without making your own copy of the templates.

  1. mkdir locale

  2. ./manage.py makemessages

  3. Edit locale/en/LC_MESSAGES/django.po, adding these lines:

    msgid "Django site admin"
    msgstr "MySite site admin"
    
    msgid "Django administration"
    msgstr "MySite administration"
    
  4. ./manage.py compilemessages

See https://docs.djangoproject.com/en/1.3/topics/i18n/localization/#message-files


回答 8

admin.py:

from django.contrib.admin import AdminSite

AdminSite.site_title = ugettext_lazy('My Admin')

AdminSite.site_header = ugettext_lazy('My Administration')

AdminSite.index_title = ugettext_lazy('DATA BASE ADMINISTRATION')

admin.py:

from django.contrib.admin import AdminSite

AdminSite.site_title = ugettext_lazy('My Admin')

AdminSite.site_header = ugettext_lazy('My Administration')

AdminSite.index_title = ugettext_lazy('DATA BASE ADMINISTRATION')

回答 9

首先,您应该将template / admin / base_site.html添加到您的项目中。该文件可以被安全地覆盖,因为它是Django开发人员专门用于自定义管理站点的确切目的的文件。这是放置在文件中的示例:

{% 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 %}

这是惯例。但是在此之后,我注意到我在主要的管理索引页面上仍然留下烦人的“站点管理”。而且此字符串不在任何模板内,而是在admin视图内设置。幸运的是,更改非常容易。假设您的语言设置为英语,请从您的项目目录中运行以下命令:

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

现在打开文件locale / en / LC_MESSAGES / django.po,并在标题信息之后添加两行(此示例的最后两行)

"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"

之后,请记住运行以下命令并重新加载项目的服务器:

$ ./manage.py compilemessages

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

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/


回答 10

Django 2.0中,您只需在中添加一行url.py并更改名称即可。

# url.py

from django.contrib import admin 
admin.site.site_header = "My Admin Central" # Add this

对于旧版本的Django。(<1.11和更早版本),您需要进行编辑admin/base_site.html

更改此行

{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}

{% block title %}{{ title }} | {{ site_title|default:_('Your Site name Admin Central') }}{% endblock %}

您可以django通过以下方式检查您的版本

django-admin --version

From Django 2.0 you can just add a single line in the url.py and change the name.

# url.py

from django.contrib import admin 
admin.site.site_header = "My Admin Central" # Add this

For older versions of Django. (<1.11 and earlier) you need to edit admin/base_site.html

Change this line

{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}

to

{% block title %}{{ title }} | {{ site_title|default:_('Your Site name Admin Central') }}{% endblock %}

You can check your django version by

django-admin --version

回答 11

只需转到admin.py文件并将此行添加到文件中:

admin.site.site_header = "My Administration"

Just go to admin.py file and add this line in the file :

admin.site.site_header = "My Administration"


回答 12

您无需为此工作更改任何模板,只需更新settings.py项目的即可。转到的底部settings.py并定义它。

admin.site.site_header = 'My Site Admin'

这样,您将能够更改Django admin的标头。此外,您可以在以下链接上阅读有关Django Admin自定义和设置的更多信息。

Django管理文档

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


回答 13

您可以AdminSite.site_header用来更改该文本。这是文档

You can use AdminSite.site_header to change that text. Here is the docs


回答 14

有两种方法可以做到这一点:

1]覆盖base_site.htmldjango/contrib/admin/templates/admin/base_site.html:以下是内容base_site.html

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

{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}

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

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

在上述代码段中编辑site_title和site_header。此方法有效,但不推荐使用,因为它是静态更改。

2]通过在urls.py项目目录中添加以下行:

admin.site.site_header = "AppHeader"
admin.site.site_title = "AppTitle"
admin.site.index_title = "IndexTitle"

推荐使用此方法,因为我们无需编辑即可更改网站标题,网站标题和索引标题base_site.html

There are two methods to do this:

1] By overriding base_site.html in django/contrib/admin/templates/admin/base_site.html: Following is the content of base_site.html:

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

{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}

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

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

Edit the site_title & site_header in the above code snippet. This method works but it is not recommendable since its a static change.

2] By adding following lines in urls.py of project’s directory:

admin.site.site_header = "AppHeader"
admin.site.site_title = "AppTitle"
admin.site.index_title = "IndexTitle"
admin.site.site_url = "Url for view site button"

This method is recommended one since we can change the site-header, site-title & index-title without editing base_site.html.


回答 15

由于我仅在应用程序中使用管理界面,因此将其放在admin.py中:

admin.site.site_header = 'My administration'

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

admin.site.site_header = 'My administration'

回答 16

您只需覆盖admin/base_site.html模板(从复制模板django.contrib.admin.templates并放入您自己的管理模板目录),然后替换该branding块。

You just override the admin/base_site.html template (copy the template from django.contrib.admin.templates and put in your own admin template dir) and replace the branding block.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。