Wintermute
Wintermute

Reputation: 3045

Unable to override django template from 3rd party app

I'm trying to override the base forum template for django-machina.

I've copied the board_base.html contents from github source into /app/forum/templates/machina/board_base.html.

My settings file contains a templates section like:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
            MACHINA_MAIN_TEMPLATE_DIR,
        ],
#snip

Django is definitely looking in that directory: if I set up a different view pointing to a nonexistent template file, then on the resulting error page the first path that Django reports trying is:

/app/forum/templates/nothing.html (Source does not exist)

What have I missed? Why can't I override that template?


Edit:

So my DIRS now looks like:

'DIRS': [
    MACHINA_MAIN_TEMPLATE_DIR,
],

and I've got a separate app, forum_templates, with its own templates dir. INSTALLED_APPS looks like:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'forum_templates',
    'core',

    # Machina related apps:
    'mptt',
    'haystack',
    'widget_tweaks',
] + get_machina_apps()

If I comment out the MACHINA_MAIN_TEMPLATE_DIR from DIRS, I get the expected TemplateDoesNotExist error, with the following paths listed:

/usr/local/lib/python3.6/site-packages/django/contrib/admin/templates/partials/breadcrumb.html
/usr/local/lib/python3.6/site-packages/django/contrib/auth/templates/partials/breadcrumb.html
/app/forum/forum_templates/templates/partials/breadcrumb.html
/app/forum/core/templates/partials/breadcrumb.html
/usr/local/lib/python3.6/site-packages/mptt/templates/partials/breadcrumb.html
/usr/local/lib/python3.6/site-packages/haystack/templates/partials/breadcrumb.html
/usr/local/lib/python3.6/site-packages/machina/templates/partials/breadcrumb.html

So I copy the relevant file from github and save it to /app/forum/forum_templates/templates/partials/breadcrumb.html; when I reload, the error moves onto the next template file.

If I then add MACHINA_MAIN_TEMPLATE_DIR back into DIRS.. it starts loading all default templates out of machina again, ignoring the overrides in my forum_templates app.

What's going on? :(

Upvotes: 2

Views: 638

Answers (2)

fekioh
fekioh

Reputation: 996

For anyone who may still comes across this, what I found a bit unclear in the docs regarding customizations, is that in order to override the templates, you should NOT add the machina/ folder in your templates/ directory. Instead, you should add the sub-apps folders directly e.g. templates/forum/forum_detail.html and NOT templates/machina/forum/forum_detail.html.

Overrides for the base files e.g. board_base.html should be placed in the root of your templates e.g. templates/board_base.html

Upvotes: 0

Yan King Yin
Yan King Yin

Reputation: 1268

This works for me:

from machina import MACHINA_MAIN_TEMPLATE_DIR

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': (
            [os.path.join(BASE_DIR, 'templates')],
            MACHINA_MAIN_TEMPLATE_DIR,
            ),
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                .... 
            ],
        },
    },
]

Now go to your base dir /templates and move the original Machina templates to this new place. Be careful of the directory levels, you may have to experiment a bit. Delete the origin Machina template dir so as to force it to find your new files.

Upvotes: 1

Related Questions