Homunculus Reticulli
Homunculus Reticulli

Reputation: 68366

Django base template location

I am creating a website using Django 2.0.7. I need to use a base template that is outside the purview of all applications, but can be inherited and used by templates in my other applications in the project.

I have edited my settings.py file appropriately (as per the documentation), but still when I go the root (i.e. home) page of my site, I get a blank page - can anyone explain why?

I have the following directory structure

myproj
    ----manage.py
    ----app1
    ----templates/
            base.html
            index.html
    ----static/
    ----myproj
            __initi__.py
            wsgi.py
            settings.py
            urls.py
            views.py # <- I added this

The relevant parts of my settings.ini file looks like this:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

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

views.py:

from django.http import HttpResponse


def index(request):
    return HttpResponse()

Why can't django find my index.html ?

Upvotes: 2

Views: 1773

Answers (1)

JLucasRS
JLucasRS

Reputation: 101

Use 'DIRS': [os.path.join(BASE_DIR, "templates")] in your settings, and your view should return a object which knows what template to render, like this one:

from django.shortcuts import render

def index(request):
    return render(request, 'index.html')

There's also TemplateResponse(), SimpleTemplateResponse() and other objects you can return, depending on your needs.

Upvotes: 3

Related Questions