Kyle
Kyle

Reputation: 355

TemplateDoesNotExist - file exists, no permissions issue

I'm receiving this error when trying to render a template in django:

TemplateDoesNotExist

...
Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:

Here's my settings.py entry:

SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
    os.path.join(SETTINGS_PATH, 'template'),
)

Here's my view that's being called:

def handler(request):
    if request.method == 'GET': 
        NewAppFormSet = modelformset_factory(Application)

    return render_to_response(request, "template/apps.html",{"new_app_form":NewAppFormSet})

I've tried every possible combination of paths, adjusting permissions, etc. The real issue seems to be with the template loaders, as they are not recognizing any paths set forth in TEMPLATE_DIRS. I've hardcoded this string, to no avail. I've also ran python manage.py runserver with sudo / as root. I'm at a loss...

Upvotes: 2

Views: 16331

Answers (6)

MickeyMak
MickeyMak

Reputation: 51

I had a very similar issue which was driving me crazy. It turns out that the book I was using to learn Python/Django did not say that I had to set the templates directory in settings.py expressly. Very confusing. I am using Python 2.6 & Django 1.3.1 - maybe a previous version automatically found the templates dir. Anyway, after pulling my hair out for a day and reading a lot on this site I figured out that I had to replace the default TEMPLATE_DIRS assignment with the following in settings.py:

# Find templates in the same folder as settings.py.
SETTINGS_PATH = os.path.realpath(os.path.dirname(__file__))

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(SETTINGS_PATH, 'templates'),
)

Thanks stackoverflow!!

Upvotes: 5

Kyle
Kyle

Reputation: 355

I'm not sure what example I was following that specified the render_to_response shortcut method requires the views' request object to be passed, but it was as easy as changing it to:

return render_to_response("template/apps.html",{"new_app_form":NewAppFormSet()})

Upvotes: 1

user221209
user221209

Reputation:

what if you try

return render_to_response(request, "apps.html",{"new_app_form":NewAppFormSet})

instead of

return render_to_response(request, "template/apps.html",{"new_app_form":NewAppFormSet})

Upvotes: 0

Ben Belchak
Ben Belchak

Reputation: 549

From my settings.py:

BASE_DIR = os.path.dirname(__file__)

def RELATIVE_PATH(*args):
    return os.path.join(BASE_DIR, *args)

TEMPLATE_DIRS = (
    RELATIVE_PATH('template'),
)

Upvotes: 0

Afrowave
Afrowave

Reputation: 940

By any chance, you might have copy-pasted the TEMPLATE_DIRS you have now, did you leave the default one lower down the SETTINGS file?

Upvotes: 0

Jeffrey Bauer
Jeffrey Bauer

Reputation: 14080

Try assigning SETTINGS_PATH with os.path.realpath:

SETTINGS_PATH = os.path.realpath(os.path.dirname(__file__))

Upvotes: 0

Related Questions