9

I am new to Django. I am using Django 1.8.6 with Python 2.7. I am trying to use a base.html template that can be used globaly through out the entire site, where every app and access it. Here is my test site's current structure:

twms

polls

migrations

static

templates

project

migrations

static

templates

project

index.html

tmws

static

templates

tmws

base.html

Here is the code for project/templates/project/index.html

{% extends 'tmws/base.html' %}
{% block content %}
    <h1>Projects</h1>
    <ul>
    {% for project in project_list %}
        <li><a href="{% url 'project:detail' project.id %}">{{ project.name }}</a></li>
    {% endfor %}

    </ul>
    end of list
{% endblock %}

This is the error I am receiving:

TemplateDoesNotExist at /project/

tmws/base.html

How do I access tmws/tmws/templates/tmws/base.html from any of my apps?

Any help would be greatly appreciated!

Let me know if any additional information is needed.

EDIT

Here are my template settings from settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),  # If i leave both or just comment one one out I still get the same error
            'tmws.tmws.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',
            ],
        },
    },
]
user908759
  • 1,355
  • 8
  • 26
  • 48

4 Answers4

17

I think you might be having a problem with your template directory configuration. In your project´s settings.py try to check if you have a configuration similar to this one:

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

Where 'your template dir name' should be tmws. This will make django search for this directory whenever you try to extend templates in your HTML. You can add as many directories as you want.

I think right now Django must be searching for the template in:

twms/project/templates/project

So maybe if you place your base.html file there Django will be able to find it.

Another suggestion would be to create a general templates directory and place your base.html there since you want it to be used in the entire site. This is just my taste for ordering templates, but it should work either way.

Edit:

Adding the following in settings.py solved the problem. Read comments for more info:

MASTER_BASE_DIR = os.path.dirname(__file__)




TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(MASTER_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',
                ],
            },
        },
    ]
Pablo Estrada
  • 3,182
  • 4
  • 30
  • 74
  • 1
    You are welcome. Can you try adding: `os.path.join(BASE_DIR, 'twms/templates')` or `os.path.join(BASE_DIR, 'twms.templates')` . I´m not sure if I can use dot notation there. – Pablo Estrada Feb 06 '16 at 23:39
  • Thank you for all your help, but I am still getting the same error. For some reason I cannot get out of the projects directory. I'm not sure if it might have something to do with BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))). I have tried many ways of manipulating this but when I get a this page is not available error – user908759 Feb 07 '16 at 00:03
  • 1
    Got it working by adding the following variable to settings.py MASTER_BASE_DIR = os.path.dirname(\_\_file\_\_) and then adding the following to Temaplates = [ ... 'DIRS':[os.path.join(MASTER_BASE_DIR, 'templates'),]]. add this to your answer and I will mark it as correct. – user908759 Feb 07 '16 at 00:11
  • Was stuck for this for a long timebecause i included a base file, thank you – shubhendu Nov 23 '17 at 12:59
0

Check the TEMPLATES value in your settings. You can add the DIR option with a directory where you can put all your common templates.

Link: https://docs.djangoproject.com/en/1.9/ref/settings/#templates

Karim N Gorjux
  • 2,880
  • 22
  • 29
0

You need to add all templates folders to the TEMPLATES settings in settings.py. Django will then treat them as if they're all in the same folder. It seems like you have not added all of them. You want your TEMPLATES to look something like this:

TEMPLATES = [
    {
        'DIRS': [
            'twms.tmws.templates',  # Since this is not in an app
        ],
        'APP_DIRS': True,  # Automatically include the templates from INSTALLED_APPS
    },
]
olofom
  • 6,233
  • 11
  • 37
  • 50
0

You set up the directories to search for the templates as follows:

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

where it should read instead:

        'DIRS': [os.path.join(MASTER_BASE_DIR, 'tmws/templates'),]

Why? because BASE_DIR is defined as follows:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#       -> os.path.dirname(os.path.dirname(/your/path/tmws/tmws/settings.py))
#       -> /your/path/tmws/

So BASE_DIR points to your top folder, if you then join it with 'templates', it becomes: /your/path/tmws/templates, which doesn't exist. However, with the changed line in the DIRS list, it will become /your/path/tmws/tmws/templates which is the correct one.

pklaus
  • 647
  • 8
  • 21