e.doroskevic
e.doroskevic

Reputation: 2167

Django - Extending Global Static HTML Template

Description

How could I extend base.html in my apps using following folder structure

Folder Structure

└───project
    ├───plots # <-- app
    ├───project 
    ├───projects # <-- app
    ├───static # <-- project static files
    │   ├───css
    │   ├───html
    │   └───img
    └───users # <-- app

Settings File

STATIC_URL = '/static/'

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

and I do use static files via {% load static %} - {% static '/css/base.css' %}

I also know how to use {% extends file %} - {% extends users/html/base.html %}

I would like to extend from static folder like such {% extends 'html/base.html' %}, however I can't find a way how to achieve this relation.

Alternative Solution

I found an alternative way to get it to work modifying templates entry in projects settings file. It works but, if possible, I would like to keep all static files in one place.

Folder Structure

└───project
    ├───plots # <-- app
    ├───project 
    ├───projects # <-- app
    ├───static # <-- project static files
    │   ├───css
    │   └───img
    ├───templates #<-- !now `base.html` is here!
    └───users # <-- app

Settings

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',
            ],
        },
    },
]

Upvotes: 1

Views: 702

Answers (1)

iklinac
iklinac

Reputation: 15758

In your Templates settings you have

DIRS defines a list of directories where the engine should look for template source files, in search order.

    'DIRS': [
        os.path.join(BASE_DIR, 'templates')
    ],

If you want your template engine to see other folders or replace current, just add them up

Also, there is a big difference between templates and static files

Upvotes: 2

Related Questions