Skip to content

Commit

Permalink
Added chapter for adding logo
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Feb 5, 2018
1 parent d15c553 commit 84c47ee
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 3 deletions.
Binary file added docs/images/logo_fixed.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/umsra_logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions docs/logo.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,64 @@
How to add a logo to Django admin?
===========================================================

Your highers ups at UMSRA loge the admin you have created till now, but marketing wants to put the UMSRA logo on all admin pages.

You need to override the default templates provided by Django. In your django settings, you 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 :code:`templates` inside each app, but you can override that by setting a value for :code:`TEMPLATES.DIRS`.

We change the code:`'DIRS': [],` to code:`'DIRS': [os.path.join(BASE_DIR, 'templates/')],`, and create the `templates` folder. If your code::`STATICFILES_DIRS` is empty set it to::

STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]

Now copy the code::`base_site.html` from the admin app to :code:`templates\admin` folder you just created. Replace thre default text in `branding` block with::

<h1 id="site-name">
<a href="{% url 'admin:index' %}">
<img src="{% static 'umsra_logo.png' %}" height="40px" />
</a>
</h1>

With the changes your base_site.html will look like this::

{% 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 'umsra_logo.png' %}" height="40px" />
</a>
</h1>
{% endblock %}

{% block nav-global %}{% endblock %}

And your admin will look like this

.. image:: images/logo_fixed.png




2 changes: 2 additions & 0 deletions docs/override_default_templates.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
How to override Django admin templates?
===========================================================

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates
4 changes: 2 additions & 2 deletions docs/two_admin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Right now our :code:`entity` and :code:`event` models are in same place. UMSRA h

We will keep the default admin for `entities` and create a new subclass of :code:`AdminSite` for `events`.

In our :code:`events/admin.py` we do
In our :code:`events/admin.py` we do::

class EventAdminSite(AdminSite):
site_header = "UMSRA Events Admin"
Expand All @@ -23,7 +23,7 @@ In our :code:`events/admin.py` we do
event_admin_site.register(EventHero)
event_admin_site.register(EventVillain)

And change the :code:`urls.py` to
And change the :code:`urls.py` to ::

from events.admin import event_admin_site

Expand Down
9 changes: 9 additions & 0 deletions heroes_and_monsters/entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ class Category(models.Model):
class Meta:
verbose_name_plural = "Categories"

def __str__(self):
return self.name


class Origin(models.Model):
name = models.CharField(max_length=100)

def __str__(self):
return self.name


class Entity(models.Model):
GENDER_MALE = "Male"
Expand All @@ -35,6 +41,9 @@ class Entity(models.Model):
)
description = models.TextField()

def __str__(self):
return self.name

class Meta:
abstract = True

Expand Down
5 changes: 4 additions & 1 deletion heroes_and_monsters/heroes_and_monsters/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates/')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand Down Expand Up @@ -121,3 +121,6 @@
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
Binary file added heroes_and_monsters/static/umsra_logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions heroes_and_monsters/templates/admin/base_site.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% 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 'umsra_logo.png' %}" height="40px" />
</a>
</h1>
{% endblock %}

{% block nav-global %}{% endblock %}

0 comments on commit 84c47ee

Please sign in to comment.