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)),
)
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:
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.
mkdir locale
./manage.py makemessages
Edit locale/en/LC_MESSAGES/django.po, adding these lines:
msgid "Django site admin"
msgstr "MySite site admin"
msgid "Django administration"
msgstr "MySite administration"
# "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')
# "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:
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')
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)
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.
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.
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
发布评论
评论(25)
从 Django 1.7 开始,您不需要覆盖模板。您现在可以实现 site_header,
site_title
和index_title
属性自定义 AdminSite 以便轻松更改管理站点页面标题和标题文本。创建一个 AdminSite 子类并将您的实例挂接到您的 URLconf 中:admin.py:
urls.py:
更新:正如 oxfn 所指出的,您可以只需设置
site_header
直接在您的urls.py
或admin.py
中,无需子类化AdminSite
:As of Django 1.7 you don't need to override templates. You can now implement site_header,
site_title
, andindex_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:
urls.py:
Update: As pointed out by oxfn you can simply set the
site_header
in yoururls.py
oradmin.py
directly without subclassingAdminSite
:有一种简单的方法来设置管理站点标头 - 将其分配给 urls.py 中的当前管理实例,如下所示
或者可以在单独的方法中实现一些标头构建魔法
因此,在简单的情况下不需要子类
AdminSite
There is an easy way to set admin site header - assign it to current admin instance in
urls.py
like thisOr one can implement some header-building magic in separate method
Thus, in simple cases there's no need to subclass
AdminSite
在 urls.py 中,您可以覆盖 3 个最重要的变量:
参考:有关这些属性的 Django 文档。
In
urls.py
you can override the 3 most important variables:Reference: Django documentation on these attributes.
更新:如果您使用的是 Django 1.7+,请参阅下面的答案。
2011 年的原始答案:
您需要创建自己的管理
base_site.html
模板才能执行此操作。最简单的方法是创建文件:这应该是 原始
base_site.html
,除了放入您的自定义标题:为此,您需要为您的项目进行正确的设置,即在
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:This should be a copy of the original
base_site.html
, except putting in your custom title:For this to work, you need to have the correct settings for your project, namely in
settings.py
:/projectdir/templates/
is added intoTEMPLATE_DIRS
.django.template.loaders.filesystem.Loader
is added intoTEMPLATE_LOADERS
.See docs for more information on
settings.py
.Django 1.8.3 中基于此问题的答案的简单完整解决方案。
在
settings.py
添加:在
urls.py
添加:A simple complete solution in Django 1.8.3 based on answers in this question.
In
settings.py
add:In
urls.py
add:对于 Django 2.1.1,将以下行添加到
urls.py
For Django 2.1.1 add following lines to
urls.py
最简单的方法
确保您有
,然后将它们添加到主应用程序的
url.py
底部The easiest way of doing it
make sure you have
and then just add these at bottom of
url.py
of you main application希望参加聚会还不算太晚,最简单的方法是编辑 admin.py 文件。
Hope am not too late to the party, The easiest would be to edit the admin.py file.
简单方法是在您的 url.py 中添加以下代码
Simple Method is add following code in your url.py
正如您在 模板,文本通过本地化框架传递(注意使用
trans
模板标签)。您可以更改翻译文件以覆盖文本,而无需制作自己的模板副本。mkdir 语言环境
./manage.py makemessages
编辑
locale/en/LC_MESSAGES/django.po
,添加以下行:./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.mkdir locale
./manage.py makemessages
Edit
locale/en/LC_MESSAGES/django.po
, adding these lines:./manage.py compilemessages
See https://docs.djangoproject.com/en/1.3/topics/i18n/localization/#message-files
从 Django 2.0 开始,您只需在
url.py
中添加一行并更改名称即可。对于旧版本的 Django。 (<1.11 及更早版本) 您需要编辑
admin/base_site.html
将此行更改
为
您可以通过以下方式检查您的
django
版本From Django 2.0 you can just add a single line in the
url.py
and change the name.For older versions of Django. (<1.11 and earlier) you need to edit
admin/base_site.html
Change this line
to
You can check your
django
version by有两种方法可以做到这一点:
1] 通过覆盖
django/contrib/admin/templates/admin/base_site.html
中的base_site.html
:以下是
base_site.html
的内容:编辑site_title &上面代码片段中的site_header。此方法有效,但不推荐,因为它是静态更改。
2] 通过在项目目录的
urls.py
中添加以下行:推荐使用此方法,因为我们可以更改站点标题、站点标题和站点标题。索引标题,无需编辑
base_site.html
。There are two methods to do this:
1] By overriding
base_site.html
indjango/contrib/admin/templates/admin/base_site.html
:Following is the content of
base_site.html
: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:This method is recommended one since we can change the site-header, site-title & index-title without editing
base_site.html
.使用
format_html
允许渲染 html,否则它将只是纯文本。在您的主
urls.py
文件中添加以下内容(urls.py
位于settings.py
所在的目录中):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 wheresettings.py
exist):admin.py:
admin.py:
只需转到 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"
您可以更改网站标题, 站点标题和 索引标题 Django Admin 如下所示:
然后,这些更改如下所示:
此外,您可以使用 gettext_lazy() 如下所示。 *gettext() 不起作用对于他们,您可以查看我的答案 解释如何在 Django 中进行翻译:
然后,这些内容的翻译如下所示:
You can change site title, site header and index title in Django Admin as shown below:
Then, these are changed as shown below:
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:
Then, these are translated as shown below:
首先,您应该将 templates/admin/base_site.html 添加到您的项目中。该文件可以安全地被覆盖,因为 Django 开发人员专门用于稍微自定义您的管理站点的目的。以下是要放入文件中的内容的示例:
这是常见做法。但我注意到此后我仍然在主管理索引页面上留下了一个烦人的“站点管理”。该字符串不在任何模板内,而是设置在管理视图内。幸运的是,它很容易改变。假设您的语言设置为英语,请从项目目录运行以下命令:
现在打开文件 locale/en/LC_MESSAGES/django.po 并在标题信息后添加两行(本示例的最后两行
) ,请记住运行以下命令并重新加载项目的服务器:
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:
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:
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)
After this, remember to run the following command and reload your project’s server:
source: http://overtag.dk/wordpress/2010/04/changing-the-django-admin-site-title/
您可以在主
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 = ""
您不需要为此工作更改任何模板,只需更新项目的
settings.py
即可。转到settings.py
的底部并定义它。通过这种方式,您将能够更改 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 thesettings.py
and define this.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
您可以使用
AdminSite.site_header
更改该文本。这是 文档You can use
AdminSite.site_header
to change that text. Here is the docs在
admin.py
文件中只需覆盖这些属性In
admin.py
file just override these attributes您只需覆盖
admin/base_site.html
模板(从django.contrib.admin.templates
复制模板并放入您自己的管理模板目录)并替换品牌
块。You just override the
admin/base_site.html
template (copy the template fromdjango.contrib.admin.templates
and put in your own admin template dir) and replace thebranding
block.由于我只在应用程序中使用管理界面,因此我将其放入 admin.py 中:
Since I only use admin interface in my app, I put this in the admin.py :
一种简单的方法是将以下代码包含在项目文件夹中的 urls.py 文件中。
例如:
One easy method is to include the following code in the urls.py file located in your project folder.
For example:
这很简单..只需转到外部文件夹..单击站点包并转到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