Question

Not the first time I get this error but usually I solve it out by adding a template path to the TEMPLATE_DIRS tuple, although this is not working for me anymore. Here's my settings.py template snippet:

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
 os.path.join(os.path.dirname(__file__), '..', 'templates').replace('\\','/'),
)

My installed apps snippet:

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
 #this is my app right here.
'boilerplate',
)

And the simple code I have in my views.py file:

from django.shortcuts import render_to_response
from django.template import RequestContext

def index(request):
 return render_to_response('test.html',context_instance=RequestContext(request))

I believe it is important to mention that my templates folder is placed within my project's structure not my app's. Any ideas?

Était-ce utile?

La solution

This is the folder structure you need to achieve.

myproject/
  ...
  myapp/
    views.py
    ...
    templates/
      myapp/
        mytemplate.html

That is what you do in the views.py

def index(request):
 return render_to_response('myapp/test.html',context_instance=RequestContext(request))

Your TEMPLATE_DIRS is alright.

Read this for further information: https://docs.djangoproject.com/en/1.6/intro/tutorial03/#write-views-that-actually-do-something

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top