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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4938491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:15:41  来源:igfitidea点击:

Django Admin - change header 'Django administration' text

pythondjangodjango-admin

提问by samurailawngnome

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

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

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

它似乎没有包含在“自定义管理员”文档中。

采纳答案by user608133

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

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



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

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

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

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

这应该是原件base_site.html的副本,除了输入您的自定义标题:

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

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

  • Make sure /projectdir/templates/is added into TEMPLATE_DIRS.
  • Make sure django.template.loaders.filesystem.Loaderis added into TEMPLATE_LOADERS.
  • 确保/projectdir/templates/已添加到TEMPLATE_DIRS.
  • 确保django.template.loaders.filesystem.Loader已添加到TEMPLATE_LOADERS.

See docs for more information on settings.py.

有关更多信息,请参阅文档settings.py

回答by Yuji 'Tomita' Tomita

You just override the admin/base_site.htmltemplate (copy the template from django.contrib.admin.templatesand put in your own admin template dir) and replace the brandingblock.

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

回答by Wedgwood

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

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

  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

  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

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

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

回答by Soroosh

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:

首先,您应该将 templates/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 %}

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)

现在打开文件 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 <[email protected]>\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/

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

回答by Reto Aebersold

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

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

admin.py:

管理.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:

网址.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 site_headerin your urls.pyor admin.pydirectly without subclassing AdminSite:

更新:正如 oxfn 所指出的,您可以简单地site_header在您的urls.pyadmin.py直接设置 ,而无需子类化AdminSite

admin.site.site_header = 'My administration'

回答by oxfn

There is an easy way to set admin site header - assign it to current admin instance in urls.pylike this

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

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

因此,在简单的情况下,不需要子类化 AdminSite

回答by kuzavas

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

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

In settings.pyadd:

另外settings.py

ADMIN_SITE_HEADER = "My shiny new administration"

In urls.pyadd:

另外urls.py

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

回答by Gregory

In urls.pyyou can override the 3 most important variables:

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"


Reference: Django documentation on these attributes.

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

回答by numahell

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

因为我只在我的应用程序中使用管理界面,所以我把它放在 admin.py 中:

admin.site.site_header = 'My administration'

回答by Chitrank Dixit

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

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

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 管理员的标题。此外,您可以在以下链接中阅读有关 Django Admin 自定义和设置的更多信息。

Django Admin Documentation

Django 管理文档