1

So ive been trying to setup a django settings module that will check the environment variable and load settings.

Heres what my settings module looks like

/templates
    home.html

/settings
    base.py
    prod.py
    dev.py
    test.py

base.py

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

TEMPLATE_DIRS = [
os.path.join(PROJECT_ROOT, "templates"),
]

urls.py

from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
url(r"^$", direct_to_template, {'template' : 'home.html' }, name="home"),
)

When I had all of my settings in one file, this worked just fine, but since I split the files up I get the error:

TemplateDoesNotExist at /

home.html

Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/Users/Tulsa/Apps/tulsa-applications-co/tulsa/tulsa/settings/templates/home.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/django/contrib/auth/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/django/contrib/admindocs/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/grappelli/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/django/contrib/admin/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/pagination/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/djangosaml2/templates/home.html (File does not exist)
/Users/Tulsa/Apps/tulsa-applications-co/tulsa/tulsa/apps/profiles/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/debug_toolbar/templates/home.html (File does not exist)

Using loader django.template.loaders.eggs.Loader:

what am i missing here?

harristrader
  • 1,181
  • 2
  • 13
  • 20

5 Answers5

8

All answers above require of configuring TEMPLATE_DIRS or TEMPLATE_LOADERS, which is not necessary and correct. You just have to put YOUR application to the INSTALLED_APPS.

For example, if your application is in the MyMegaApp (where settigs.py resides), in other words you have a project structure like below

MyMegaApp
   MyMegaApp
      templates
         index.html
      settings.py 
   manage.py

Then you have to add your app like this

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',
    'MyMegaApp'
)

After that your index.html template will be found in MyMegaApp/templates folder

3

The problem here is, in your settings, your PROJECT_ROOT evaluates to the directory which runs manage.py.

You may do this for TEMPLATE_DIRS settings

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
print PROJECT_ROOT    

Now, append ../../ relative to the PROJECT_ROOT. Something like this:

PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))

TEMPLATE_DIRS = [
    os.path.join(PROJECT_ROOT, "templates"),
]
karthikr
  • 97,368
  • 26
  • 197
  • 188
2

Change your PROJECT_ROOT to:

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

And make sure your TEMPLATE_LOADERS variable is set properly.

Explanation:

abspath gives you the full path of the base.py, i.e., /home/some-path/project-folder/settings/base.py

Therefore, first dirname gives you the dir path name of the given path (obtained above), i.e., /home/some-path/project-folder/settings/

And then, the second dirname gives you the dir path name of the given path (obtained above), i.e., /home/some-path/project-folder/

So, now when you join this path to templates, everything starts working fine.

For more refer python docs.

Sandip Agarwal
  • 1,890
  • 5
  • 28
  • 42
  • Hey, thanks for generously donating your time to this issue. This solution worked instantly.Would you mind explaining why this solution worked so I can add this to my toolbox? – harristrader Sep 17 '12 at 23:42
  • Ya... Definitely... I am adding the explanation to the answer itself. – Sandip Agarwal Sep 18 '12 at 04:55
  • Hey Sandip so based on the above scheme how should my STATIC_ROOT and STATICFILES_DIRS variable be updated? Havent been able to get my static assets implemented. – harristrader Oct 16 '12 at 20:37
  • @harristrader Have you served your static files by specifying their urls? Moreover, please specify the problem situation here or creating a new question. – Sandip Agarwal Oct 18 '12 at 05:16
0

can you test as below?

i think your code would set PROJECT_ROOT as '/some/path/to/settings'

from os.path import dirname, abspath, normpath, join
PROJECT_ROOT = dirname(dirname(abspath(__file__)))
TEMPLATE_DIRS = (
normpath(join(PROJECT_ROOT, 'templates')),
)
Ju2017
  • 38
  • 1
  • 6
0

Include this in settings

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(SITE_ROOT, 'appname/templates'),

)

Add this to urls.py

from django.conf.urls.defaults import *
from  django.views.generic.simple.direct_to_template import direct_to_template
from appname.views import *

call url by

  urlpatterns = patterns('django.views.generic.simple',
    (r'^foo/$',             'direct_to_template', {'template': 'foo_index.html'}),
    (r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template': 'foo_detail.html'}),
  )
tom joy
  • 411
  • 8
  • 22