哪个模板在Django管理页面的彩色水平横幅下显示“网站管理”字样?

时间:2012-03-28 07:51:45

标签: django django-admin

我想在管理页面下将“网站管理”改为其他内容。我尝试了几个grep命令,但找不到合适的模板:

hobbes3@hobbes3 ~/Sites/mysite/site-packages/django/contrib/admin/templates $ grep -ri "site administration" .
hobbes3@hobbes3 ~/Sites/mysite/site-packages/django/contrib/admin/templates $ grep -ri "administration" .
./admin/base_site.html:<h1 id="site-name">{% trans 'Django administration' %}</h1>
hobbes3@hobbes3 ~/Sites/mysite/site-packages/django/contrib/admin/templates $ grep -ri "site" .
./admin/404.html:{% extends "admin/base_site.html" %}
./admin/500.html:{% extends "admin/base_site.html" %}
./admin/500.html:<p>{% trans "There's been an error. It's been reported to the site administrators via e-mail and should be fixed shortly. Thanks for your patience." %}</p>
./admin/auth/user/change_password.html:{% extends "admin/base_site.html" %}
./admin/base_site.html:{% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %}
./admin/base_site.html:<h1 id="site-name">{% trans 'Django administration' %}</h1>
./admin/change_form.html:{% extends "admin/base_site.html" %}
./admin/change_form.html:    {% if has_absolute_url %}<li><a href="../../../r/{{ content_type_id }}/{{ object_id }}/" class="viewsitelink">{% trans "View on site" %}</a></li>{% endif%}
./admin/change_list.html:{% extends "admin/base_site.html" %}
./admin/delete_confirmation.html:{% extends "admin/base_site.html" %}
./admin/delete_selected_confirmation.html:{% extends "admin/base_site.html" %}
./admin/edit_inline/stacked.html:    {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
./admin/edit_inline/tabular.html:          {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
./admin/index.html:{% extends "admin/base_site.html" %}
./admin/invalid_setup.html:{% extends "admin/base_site.html" %}
./admin/login.html:{% extends "admin/base_site.html" %}
./admin/object_history.html:{% extends "admin/base_site.html" %}
./admin/object_history.html:    <p>{% trans "This object doesn't have a change history. It probably wasn't added via this admin site." %}</p>
./registration/logged_out.html:{% extends "admin/base_site.html" %}
./registration/logged_out.html:<p>{% trans "Thanks for spending some quality time with the Web site today." %}</p>
./registration/password_change_done.html:{% extends "admin/base_site.html" %}
./registration/password_change_form.html:{% extends "admin/base_site.html" %}
./registration/password_reset_complete.html:{% extends "admin/base_site.html" %}
./registration/password_reset_confirm.html:{% extends "admin/base_site.html" %}
./registration/password_reset_done.html:{% extends "admin/base_site.html" %}
./registration/password_reset_email.html:{% blocktrans %}You're receiving this e-mail because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}
./registration/password_reset_email.html:{% trans "Thanks for using our site!" %}
./registration/password_reset_email.html:{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
./registration/password_reset_form.html:{% extends "admin/base_site.html" %}

我在错误的地方看某事吗?

修改

经过一番挖掘后,我认为它与{% block content_title %}{% endblock %}内的base.html有关...

编辑2:

以下是我在管理员主页上讨论的“网站管理”字样的截图。

3 个答案:

答案 0 :(得分:3)

让文档阅读; admin docscustomising look and feel

这是一个为管理员设置模板的简单案例,它会覆盖默认值;

  

要自定义的模板是admin / index.html。 (与上一节中的admin / base_site.html相同 - 将其从默认目录复制到自定义模板目录。)编辑文件,您将看到它使用名为app_list的模板变量。该变量包含每个已安装的Django应用程序。您可以以您认为最好的方式将链接硬编码到特定于对象的管理页面,而不是使用它。同样,如果您不能理解模板语言,请不要担心 - 我们将在教程3中更详细地介绍它。

我浏览了几周之前链接到的Django文档中的所有教程,但无法找到自定义管理模板的示例,但是这个网站看起来像它肯定会帮助您理解它;

http://blog.montylounge.com/2009/07/5/customizing-django-admin-branding/

答案 1 :(得分:3)

标题_("Site administration")定义在sites.py下的contrib/admin/文件中。您可以在那里更改该字符串,并且每次升级Django时都必须记住更改它(不推荐),或者覆盖base_site.html模板并在那里手动添加标题。

答案 2 :(得分:0)

以为我会为这个旧问题添加一个更新的参考资料,因为它仍然被查看和激活,我发现这些文档非常彻底。

要重命名模板变量的子类AdminSite

  

myapp/admin.py

from django.contrib.admin import AdminSite

from .models import MyModel

class MyAdminSite(AdminSite):
    # this is the variable you asked about
    site_header = 'Monty Python administration'

admin_site = MyAdminSite(name='myadmin')
admin_site.register(MyModel)
  

myproject/urls.py

from django.conf.urls import url

from myapp.admin import admin_site

urlpatterns = [
    url(r'^myadmin/', admin_site.urls),
]

参考:https://docs.djangoproject.com/en/dev/ref/contrib/admin/#customizing-adminsite

您的admin_site上的properties you can customise为:

site_header, site_title, site_url, index_title, index_template,
app_index_template, empty_value_display, login_template, login_form, 
logout_template, password_change_template, password_change_done_template

扩展管理模板

此外,您还可以扩展默认管理模板:

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

{% block object-tools-items %}
<p>
    Let's nuke this block and add custom content!
    The rest of the default change_form template remains the same.
</p>
{% endblock %}

参考:https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates

另一个例子是添加自定义样式表:

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

{% block extrahead %}
    <link rel="stylesheet" href="{% static "my-app/admin-custom.css" %}">
{% endblock %}

这显示了对所有 change_form实例的覆盖,但您也可以覆盖单个ModelAdmin和TabularInline等类中的模板。

整个django-admin都有相当多的继承,所以安装Django Debug Toolbar对于确定扩展内容(以及你的覆盖应放在templates目录中的位置)非常有帮助。