Django 1.9 Template Setup
Django 1.9 came out out in December, and today I felt the pains of trying to work with recently updated code. Turns out a new version can really compromise the reliability of StackOverflow answers. I’m sure it would have been less painful were I more familiar with the framework, but here I am, on the other side to tell you the super simple answer to the question I spent a significant portion of my day figuring out.
We were setting up the landing page of a brand new project. We created a templates directory in the project root, and added an index.html file welcoming users. We then created a views.py and set up a method to handle requests:
return render(request, 'index.html')
Every time we started up the server, however, we got a TemplateDoesNotExist error, informing us that there was no such thing as
Django was right, those weren’t valid file locations, but they also weren’t places we were trying to reference. Even changing our references to index.html to very explicit, long file paths didn’t help, Django was still looking in admin and auth. Long story short, after many unsuccessful attempts based on how earlier versions of Django worked, we realized that our settings.py needed a 1 line change. Here’s the Templates configuration that Django auto-generated for us:
TEMPLATES = (
The 4th line is setting the DIRS variable is set to an empty array. This should actually be setup to point to the location of the templates directory:
'DIRS': [BASE_DIR + '/templates'],
THAT’S ALL. As you can see, in the absence of DIRS, Django looks for templates in the listed context processors.