如何在 Django Admin 中更改站点标题、站点标题和索引标题?

发布于 2024-10-16 18:58:15 字数 272 浏览 2 评论 0 原文

如何在 Django Admin 中更改网站标题 Django site admin、网站标头 Django Administration 和索引标题 Site Administration

输入图像描述这里

How can I change the site title Django site admin, the site header Django administration and the index title Site administration in Django Admin?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(25

请远离我 2024-10-23 18:58:15

从 Django 1.7 开始,您不需要覆盖模板。您现在可以实现 site_headersite_titleindex_title 属性自定义 AdminSite 以便轻松更改管理站点页面标题和标题文本。创建一个 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 所指出的,您可以只需设置 site_header 直接在您的 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 site_header in your urls.py or admin.py directly without subclassing AdminSite:

admin.site.site_header = 'My administration'
心安伴我暖 2024-10-23 18:58:15

有一种简单的方法来设置管理站点标头 - 将其分配给 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

安静 2024-10-23 18:58:15

在 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.

烟火散人牵绊 2024-10-23 18:58:15

更新:如果您使用的是 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 中。

有关 settings.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.

亣腦蒛氧 2024-10-23 18:58:15

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
中性美 2024-10-23 18:58:15

对于 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'
几味少女 2024-10-23 18:58:15

最简单的方法
确保您有

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" 
迷鸟归林 2024-10-23 18:58:15

希望参加聚会还不算太晚,最简单的方法是编辑 admin.py 文件。

admin.site.site_header = 'your_header'
admin.site.site_title = 'site_title'
admin.site.index_title = 'index_title'

Hope am not too late to the party, The easiest would be to edit the admin.py file.

admin.site.site_header = 'your_header'
admin.site.site_title = 'site_title'
admin.site.index_title = 'index_title'
儭儭莪哋寶赑 2024-10-23 18:58:15

简单方法是在您的 url.py 中添加以下代码

    urlpatterns = [
    #Your URLS here
    path('admin/', admin.site.urls),
]

admin.site.site_header = 'SiteName Admin'
admin.site.site_title  = 'SiteName'
admin.site.index_title   = 'Admin'

Simple Method is add following code in your url.py

    urlpatterns = [
    #Your URLS here
    path('admin/', admin.site.urls),
]

admin.site.site_header = 'SiteName Admin'
admin.site.site_title  = 'SiteName'
admin.site.index_title   = 'Admin'
她如夕阳 2024-10-23 18:58:15

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

  1. mkdir 语言环境

  2. ./manage.py makemessages

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

    msgstr "Django 站点管理员"
    msgstr "MySite 站点管理员"
    
    msgstr "Django 管理"
    msgstr "MySite 管理"
    
  4. ./manage.pycompilemessages

请参阅 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

反话 2024-10-23 18:58:15

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
计㈡愣 2024-10-23 18:58:15

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

1] 通过覆盖 django/contrib/admin/templates/admin/base_site.html 中的 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"
admin.site.site_url = "Url for view site button"

推荐使用此方法,因为我们可以更改站点标题、站点标题和站点标题。索引标题,无需编辑 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.

苍暮颜 2024-10-23 18:58:15

使用 format_html 允许渲染 html,否则它将只是纯文本。

在您的主 urls.py 文件中添加以下内容(urls.py 位于 settings.py 所在的目录中):

from django.contrib import admin
from django.utils.html import format_html

site_header = 'Your html snippet'
admin.site.site_header = format_html(site_header)

Use format_html to allow html to be rendered, otherwise it will be just plain text.

in your main urls.py file add followings(urls.py is in the directory where settings.py exist):

from django.contrib import admin
from django.utils.html import format_html

site_header = 'Your html snippet'
admin.site.site_header = format_html(site_header)
晚雾 2024-10-23 18:58:15

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')
抽个烟儿 2024-10-23 18:58:15

只需转到 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"

梦罢 2024-10-23 18:58:15

您可以更改网站标题, 站点标题索引标题 Django Admin 如下所示:

# "admin.py"

from django.contrib import admin

admin.site.site_title = 'My site title'
admin.site.site_header = 'My site header'
admin.site.index_title = 'My index title'

然后,这些更改如下所示:

在此处输入图像描述

此外,您可以使用 gettext_lazy() 如下所示。 *gettext() 不起作用对于他们,您可以查看我的答案 解释如何在 Django 中进行翻译:

# "admin.py"

from django.contrib import admin
from django.utils.translation import gettext_lazy as _

admin.site.site_title = _('My site title')
admin.site.site_header = _('My site header')
admin.site.index_title = _('My index title')

然后,这些内容的翻译如下所示:

在此处输入图像描述

You can change site title, site header and index title in Django Admin as shown below:

# "admin.py"

from django.contrib import admin

admin.site.site_title = 'My site title'
admin.site.site_header = 'My site header'
admin.site.index_title = 'My index title'

Then, these are changed as shown below:

enter image description here

In addition, you can translate them with gettext_lazy() as shown below. *gettext() doesn't work for them and you can see my answer explaining how to translate in Django:

# "admin.py"

from django.contrib import admin
from django.utils.translation import gettext_lazy as _

admin.site.site_title = _('My site title')
admin.site.site_header = _('My site header')
admin.site.index_title = _('My index title')

Then, these are translated as shown below:

enter image description here

你的心境我的脸 2024-10-23 18:58:15

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

这是常见做法。但我注意到此后我仍然在主管理索引页面上留下了一个烦人的“站点管理”。该字符串不在任何模板内,而是设置在管理视图内。幸运的是,它很容易改变。假设您的语言设置为英语,请从项目目录运行以下命令:

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

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

$ ./manage.py compilemessages

source: 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 <[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/

眼前雾蒙蒙 2024-10-23 18:58:15

您可以在主 urls.py 中使用以下行,

您可以在要显示的引号中添加文本

要替换文本 Django admin 使用 admin.site.site_header = ""

要替换文本站点管理,请使用 admin.site.site_title = ""

要替换站点名称,您可以使用 admin.site.index_title = ""

要替换您可以使用的查看站点按钮的 url admin.site.site_url = ""

You can use these following lines in your main urls.py

you can add the text in the quotes to be displayed

To replace the text Django admin use admin.site.site_header = ""

To replace the text Site Administration use admin.site.site_title = ""

To replace the site name you can use admin.site.index_title = ""

To replace the url of the view site button you can use admin.site.site_url = ""

非要怀念 2024-10-23 18:58:15

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

admin.site.site_header = 'My Site Admin'

通过这种方式,您将能够更改 Django 管理的标头。此外,您可以在以下链接中阅读有关 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

撕心裂肺的伤痛 2024-10-23 18:58:15

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

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

┈┾☆殇 2024-10-23 18:58:15

admin.py 文件中只需覆盖这些属性

from django.contrib import admin

admin.site.site_header = "site header name"
admin.site.index_title = "index title name"
admin.site.site_title = "site title name"

In admin.py file just override these attributes

from django.contrib import admin

admin.site.site_header = "site header name"
admin.site.index_title = "index title name"
admin.site.site_title = "site title name"
花海 2024-10-23 18:58:15

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

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.

小霸王臭丫头 2024-10-23 18:58: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'
柒夜笙歌凉 2024-10-23 18:58:15

一种简单的方法是将以下代码包含在项目文件夹中的 urls.py 文件中。

 in urls.py add 

  admin.site.site_header  =  "Site Header here"  
  admin.site.site_title  =  "Side title"
  admin.site.index_title  =  "Index title"

例如:

from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from django.conf.urls.static import static
from django.conf import settings


admin.site.site_header  =  "Hadramawt Kitchen"  
admin.site.site_title  =  "Hadramawt Kitchen"
admin.site.index_title  =  "Hadramawt Admin"

urlpatterns = [
  path('admin/', admin.site.urls),
  path('', include('food_products.urls' )),
  path('categories', include('categories.urls' )),
  path('news', include('news.urls')),
  path('weeklyOffer', include('weekly_offer.urls'))
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

One easy method is to include the following code in the urls.py file located in your project folder.

 in urls.py add 

  admin.site.site_header  =  "Site Header here"  
  admin.site.site_title  =  "Side title"
  admin.site.index_title  =  "Index title"

For example:

from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from django.conf.urls.static import static
from django.conf import settings


admin.site.site_header  =  "Hadramawt Kitchen"  
admin.site.site_title  =  "Hadramawt Kitchen"
admin.site.index_title  =  "Hadramawt Admin"

urlpatterns = [
  path('admin/', admin.site.urls),
  path('', include('food_products.urls' )),
  path('categories', include('categories.urls' )),
  path('news', include('news.urls')),
  path('weeklyOffer', include('weekly_offer.urls'))
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
思慕 2024-10-23 18:58:15

这很简单..只需转到外部文件夹..单击站点包并转到contrib文件夹..选择管理文件夹并找到sites.py并更改所有站点名称和标题

It is very easy.. just go to the external files folder..click on sites-packages and go to contrib folder..shoose admin folder and find sites.py and change all the site name and title

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文