Django 1.10: base_site.html override not working
Asked Answered
R

2

5

I am creating a site in Django with a custom view, and want to link to that view on the admin page. But even though I've followed the directions to override base_site.html in the Django tutorial, nothing changes. No matter if input the simplest change:

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

{% block title %}{{ title }} | {{ site_title|default:_('Django site admin')    }}{% endblock %}

 {% block branding %}
 <h1 id="site-name"><a href="{% url 'admin:index' %}">Test</a></h1>
 {% endblock %}

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

Or even something very drastic, where I don't extend the base.html at all:

<h1>Test</h1>

My directories are exactly as they should be, with the new base_site.html inside them:

└─myproject
    └── myproject
        └── templates
            └── admin
                └── base_site.html

This is my current settings.py:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = <mysecretkey>

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'easy_thumbnails',
   'filer',
   'mptt',
   'myapp',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
 ]

ROOT_URLCONF = 'myproject.urls'

TEMPLATES = [
   {
       'BACKEND': 'django.template.backends.django.DjangoTemplates',
       'DIRS': [os.path.join(BASE_DIR, 'templates')],
       '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',
           ],
       },
   },
]

WSGI_APPLICATION = 'myproject.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME':    'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'

# Thumbnail settings for retina displays
THUMBNAIL_HIGH_RESOLUTION = True

# Thumbnail processing

THUMBNAIL_PROCESSORS = (
    'easy_thumbnails.processors.colorspace',
    'easy_thumbnails.processors.autocrop',
    #'easy_thumbnails.processors.scale_and_crop',
    'filer.thumbnail_processors.scale_and_crop_with_subject_location',
    'easy_thumbnails.processors.filters',
 ) 

I've restarted the server, as well as used different browsers, and none of my changes show up.

Am I doing something wrong anywhere, or are the instructions given by the tutorial just plain incorrect?

Rocambole answered 23/2, 2017 at 12:56 Comment(0)
J
16

The Django tutorial is actually correct and works fine. Here's the issue: The "templates" directory is a child of the first "mysite" directly, not the second.

This is actually made very clear in the tutorial but is easy to get confused and place the "templates" directly within the wrong directory.

enter image description here

In other words, you want "templates" to be here:

mysite/templates

and not here:

mysite/mysite/templates

Tutorial step #7 makes this clear. Here's a screen shot of the directory structure as it should be at that point in time:

enter image description here

Just move your "templates" directory up one level and all should be well.

Judaism answered 17/3, 2017 at 23:26 Comment(1)
Quick tip for someone else coming here for answer: You also have to edit the TEMPLATES value in settings.py with 'DIRS': [os.path.join(BASE_DIR, 'templates')]Chiquita
A
2

In comparing your example to my working Django 1.10 project I find two differences:

  1. My file tree includes an application for the project files:

my_project --my_project ----templates ------admin

Guessing this doesn't make any difference

  1. In the TEMPLATES DIRS property, I include the name the parent directory of the templates folder:

'DIRS': [join(BASE_DIR, 'my_project/templates').replace ('\\','/'),],

Please give this a try

Ampersand answered 23/2, 2017 at 14:11 Comment(2)
Oh, my file tree is the same, I forgot to put the first my project dir in the tree. Editing.Rocambole
This works, but I will be waiting a little bit to see if there's an answer with less reference to the internal filetree.Rocambole

© 2022 - 2024 — McMap. All rights reserved.