Programming

Django Admin-헤더 'Django Administration'텍스트 변경

procodes 2020. 5. 15. 22:38
반응형

Django Admin-헤더 'Django Administration'텍스트 변경


django 관리 헤더의 'Django 관리'텍스트를 어떻게 변경합니까?

"관리자 사용자 정의"문서에서 다루지 않는 것 같습니다.


업데이트 : 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 .


Django 1.7부터 템플릿을 재정의 할 필요가 없습니다. 이제 구현할 수 site_header , site_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.py또는 admin.py서브 클래스하지 않고 직접 AdminSite:

admin.site.site_header = 'My administration'

관리 사이트 헤더를 설정하는 쉬운 방법이 있습니다-다음 urls.py과 같이 현재 관리 인스턴스에 할당하십시오

admin.site.site_header = 'My admin'

또는 별도의 방법으로 헤더 작성 마법을 구현할 수 있습니다

admin.site.site_header = get_admin_header()

따라서 간단한 경우에는 서브 클래스가 필요하지 않습니다. AdminSite


에서 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.


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

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" 

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'

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


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')

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/


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


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


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

admin.site.site_header = 'My administration'

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.

참고URL : https://stackoverflow.com/questions/4938491/django-admin-change-header-django-administration-text

반응형