问题
I have generated a django-admin for my app and I can access the dashboard. But it contains a logo that says "django admin". I want to change it to my own custom logo. How can I do that?
I have tried adding a base.html file to admin directory and tried to override but for some reason it's not working. It's code is as follows:
{% extends "admin/base.html" %}
{% load theming_tags %}
{% load staticfiles %}
{% block blockbots %}
    {{ block.super }}
    {# Using blockbots ensures theming css comes after any form media and other css #}
    {% render_theming_css %}
    <style type="text/css">
    #header #branding h1 {
        background-image: url("bootstrap_admin/img/logo-140x60.png");
    }
    </style>
{% endblock %}
{% block branding %}
<a href="{% url 'admin:index' %}" class="django-admin-logo">
    <!-- Django Administration -->
    <img src="{% static "bootstrap_admin/img/logo-140x60.png" %}" alt="{{ site_header|default:_('Django Admin') }}">
</a>
{% endblock branding %}
I want to change the logo in the top-left corner. How can I achieve what I'm trying to?
回答1:
your question is answered here
"{% static "bootstrap_admin/img/logo-140x60.png" %}"
this comes from here
django-admin-bootstrap/bootstrap_admin/static/bootstrap_admin/img/logo-140x60.png
after replacing you need to run command python manage.py collectstaticthen this will work
回答2:
The official way to achieve that is:
You need to override the default templates provided by Django. In your Django settings, your code:: TEMPLATES setting looks like this.
TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},
]
This means that Django will look for templates in a directory called templates inside each app, but you can override that by setting a value for TEMPLATES.DIRS.
We change the 'DIRS': [], to 'DIRS': [os.path.join(BASE_DIR, 'templates/')], and create the templates folder. If your STATICFILES_DIRS is empty set it to:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
Now create a file called base_site.html from the admin app to templates\admin folder you just created. Add the code in it:
{% extends "admin/base.html" %}
{% load staticfiles %}
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% 
endblock %}
{% block branding %}
<h1 id="site-name">
<a href="{% url 'admin:index' %}">
    <img src="{% static 'Your image.png' %}" height="40px" />
</a>
 </h1>
 {% endblock %}
 {% block nav-global %}{% endblock %}
回答3:
The easiest way to achieve that is
- Go to venv\lib\site_packages\bootstrap_admin\static\bootstrap_admin\img in your project folder and add your custom logo
- Go to venv\lib\site_packages\bootstrap_admin\templates\admin and open base.html file
- Locate <img src="{% static "bootstrap_admin/img/logo-140x60.png" %}" width="55" alt="{{ site_header|default:_('Django administration') }}
- Change the name 'logo-140x60.png' to the name of your custom logo
which you have added and in your settings.py add
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
- Run python manage.py collectstaticcommand
- Refresh your site and then you can see the custom logo added
来源:https://stackoverflow.com/questions/46198820/replacing-django-admins-bootstrap-themes-default-logo