1

I know this has been covered already but none of the solutions have worked for me. I have a basic django project and it cannot find the template. Here is my views.py:

now = datetime.datetime.now()
t = get_template('index.html')
html = t.render(Context({'current_date' : now}))
return HttpResponse(html)

This is my TEMPLATE_DIRS in settings.py. The project is specified under INSTALLED_APPS

TEMPLATE_DIRS = (
    '/Users/Howie/Desktop/djangoCode/mysite/Movies/templates/',
)

This is the error I get every time

Template-loader postmortem:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
C:\Users\Howie\Desktop\djangoCode\mysite..\templates\index.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
C:\Python34\lib\site-packages\django-1.7.2-py3.4.egg\django\contrib\admin\templates\index.html (File does not exist)
C:\Python34\lib\site-packages\django-1.7.2-py3.4.egg\django\contrib\auth\templates\index.html (File does not exist)
C:\Users\Howie\Desktop\djangoCode\mysite\Movies\templates\index.html (File does not exist)

I don't understand why django cant find my index.html file. I've tried moving the templates folder into the app itself(named Movies) and I've also had it in the root project folder but both of those didn't work. Any insight would be helpful.

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
Howard Peters
  • 11
  • 1
  • 1
  • 3

2 Answers2

3

The app_directories.Loader is enabled by default. This means you can put your template in an app/templates directory and be done. No need for TEMPLATE_DIRS and/or TEMPLATE_LOADERS settings. Just out of the box Django:

project/app/views.py
project/app/templates/index.html

get_template('index.html')

It's even better to use subdirectories:

project/app/views.py
project/app/templates/app/index.html

get_template('app/index.html')

This to avoid a index.html from some other app to take precedence.

If you really want a template folder in the root of your project, than don't use hardcoded paths. Do something like this:

from os.path import abspath, dirname, join, normpath
from sys import path

SITE_ROOT = dirname(dirname(abspath(__file__)))
TEMPLATE_DIRS = (
    normpath(join(SITE_ROOT, 'templates')),
)
allcaps
  • 10,945
  • 1
  • 33
  • 54
0

i had the same problem only to find out it was a typo error, i wrote templates

template/file_name/

as template which is not the Django way. The "s" is necessary

templates/file_name/

chima
  • 1
  • 1