0

I have a directory structure like this if it matters (it's the default recommended structure as per satchmo documentation):

site
- apps
   | - __init__.py
- config
- projects
   | - site
        | - home
             | - templates
                  | - about.html
                  | - home.html
             | - models.py, views.py, admin.py
        | - __init__.py
        | - local_settings.py
        | - settings.py
        | - urls.py
        | - wsgi.py
   | - __init__.py
- static
   | - css
   | - images (maybe this got autogenerated?)
   | - js
   | - media
- templates
   | base.html
- manage.py

My URLs have entries for the about.html and home.html and both of those extend base.html. However, when I visit the URLs, I get the generic satchmo page with some of my text that I had included from about and home, but it did not extend base.html at all. Before I installed satchmo, I could confirm that this was working, but now I'm not sure what went wrong. I'm assuming that it's extending some other base.html instead because if I change my extends to master.html instead, it throws a TemplateDoesNotExist exception (which I'm also unsure of how to resolve). I have the following in my settings.py:

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

TEMPLATE_DIRS = (
    'templates',
)

If I move the templates directory to the site folder within projects, it seems to work, but I don't want it there. I tried adding '../../templates' to TEMPLATE_DIRS, but that doesn't work either, and even if it did, I'm not sure how that would interact for templates that I declare under some levels of the app folder. What's the correct way to fix this?

Lunyx
  • 3,164
  • 6
  • 30
  • 46

1 Answers1

2

TEMPLATE_DIRS entries should be absolute paths. You can do something like this:

import os
from os.path import normpath, abspath, dirname, join
BASE_DIR = normpath(abspath(join(dirname(__file__), '..', '..')))
TEMPLATE_DIRS = (
    join(BASE_DIR, 'templates'),
)

If your master.html is in your templates directory, that error should be fixed, too.

The is 'basis' of the BASE_DIR is dirname(__file__), which returns the directory containing the current file, settings.py. Then, the result is joined with '..' twice, that is, we go two directories up, so now we are in the top 'site' directory. We call abspath to make sure that it's an absolute path and normpath to remove double slashes etc.

sk1p
  • 6,645
  • 30
  • 35
  • Can you explain what the BASE_DIR assignment actually does? I'm new to Python and I'm not really sure. I also already have this assignment that I copied from somewhere `BASE_DIR = os.path.dirname(os.path.dirname(__file__))`. How does this affect things? – Lunyx Jan 14 '14 at 04:10
  • I just put both through the debugger and the result for BASE_DIR was identical. It also doesn't resolve the issue. Edit: I assume that your assignment is supposed to go up one directory from what I already have, but it doesn't seem to work. – Lunyx Jan 14 '14 at 04:16
  • The one-dir-up-ness of the `BASE_DIR` should work, I got this exact code in production. But looking at it again, you need to go two dirs up (`site/projects/site/settings.py` and `site/templates/`, so `projects/site` needs to be 'removed' from the dirname of settings.py to get the absolute path of `site` as `BASE_DIR`) – sk1p Jan 14 '14 at 04:31
  • Ok, so I didn't notice that it was being overridden in `local_settings.py`. It does work now. Btw, is it better to have such settings as global only since it shouldn't change after pushing to production? I copied these settings from elsewhere, so I didn't want to break anything initially. – Lunyx Jan 14 '14 at 04:39
  • How are you using `local_settings.py`? Are you doing `from local_settings import *` in `settings.py` or the other way around? Do you have both files under source control? – sk1p Jan 14 '14 at 04:43
  • The first is how I'm using it. I haven't set up source control yet. Trying to get a working prototype first. – Lunyx Jan 14 '14 at 04:45