PythonAnywhere Forums

TemplateDoesNotExist

Trying to launch my first web app on PA and things just aren't working when going from local to PA for some reason.

My Template path is settings.py is

 import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [TEMPLATE_PATH],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},

]

and my app is included in the installed_apps section of settings.py. How do I fix this?

TemplateDoesNotExist at /Kappa/

Kappa/index.html

Request Method:     GET
Request URL:    http://ktruong88.pythonanywhere.com/Kappa/
Django Version:     1.8.4
Exception Type:     TemplateDoesNotExist
Exception Value:

Kappa/index.html

Exception Location:     /home/KTruong88/.virtualenvs/Kappa/lib/python3.4/site-packages/django/template/loader.py in get_template, line 46
Python Executable:  /usr/local/bin/uwsgi
Python Version:     3.4.0
Python Path:

['/var/www',
 '.',
 '',
 '/var/www',
 '/home/KTruong88/.virtualenvs/Kappa/lib/python3.4',
 '/home/KTruong88/.virtualenvs/Kappa/lib/python3.4/plat-x86_64-linux-gnu',
 '/home/KTruong88/.virtualenvs/Kappa/lib/python3.4/lib-dynload',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/home/KTruong88/.virtualenvs/Kappa/lib/python3.4/site-packages',
 '/home/KTruong88/Kappa_Ranks/']

Server time:    Sun, 4 Oct 2015 15:40:03 +0000

If you go to the web page, you can see some stuff that's there to help you debug stuff like this. Just underneath the error you quote, there's this bit:

Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/KTruong88/Kappa_Ranks/templates/Kappa/index.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/home/KTruong88/.virtualenvs/Kappa/lib/python3.4/site-packages/django/contrib/admin/templates/Kappa/index.html (File does not exist)
/home/KTruong88/.virtualenvs/Kappa/lib/python3.4/site-packages/django/contrib/auth/templates/Kappa/index.html (File does not exist)

So, Django is telling you exactly where it looked for your template Kappa/index.html. The first one is the place it looked based on the TEMPLATE_PATH you set -- it's looking for the file inside /home/KTruong88/Kappa_Ranks/templates/. The next two attempts were looking inside your installed Django apps. It only lists two, the standard Django admin and Django auth apps. So if your template file is inside the template subdirectory of one of your own apps, then that suggests that your app isn't on the INSTALLED_APPS list in your Django settings.

I still don't get what I'm supposed to change here. When I look at /home/KTruong88/Kappa_Ranks/templates/Kappa folder, I see index.html clearly there. I check my settings.py file too and it definitely is in the installed apps section too at /home/KTruong88/Kappa_Ranks/Kappa_Ranks/settings.py.

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Kappa',
)

I also edited my static files section with /templates/ as url and the path as /home/KTruong88/Kappa_Ranks/templates.

Perhaps it's a case problem? On PythonAnywhere (like most Unix-based environments) directory names are case sensitive; if your local setup is on Windows or another case-insensitive OS then things would work there but wouldn't here.

So, for example, if your template is in /home/KTruong88/Kappa_Ranks/templates/kappa rather than /home/KTruong88/Kappa_Ranks/templates/Kappa then the template could be found on Windows, but not on Unix.

If you're sure that's not the problem, can I take a look at your code? We can see it from our side but we always ask permission first.

I would love for you to take a look at my code. I did a double check and I've kept my project names and app names capitalized and I don't remember changing any names/capitalizations when I cloned it from github either.

It's the case of the templates directory inside /home/KTruong88/Kappa_Ranks/ -- it's called Templates with a capital T, not templates. If you rename it then it should all work fine.

The complicated thing is going to be fixing that in Git, which can be a bit awkward when you want to change the case of a directory name. I've found the best way is to do it as two separate checkins; firstly git mv the "Templates" directory to something like "temp-templates", commit that, and then git mv "temp-templates" to "templates", then commit that.

My heart just skipped. It works. Thanks for the help, I found out the templates was supposed to be capitalized.

Edit: sorry, should have triple checked before posting again. Will definitely fix the git thing too right now

Excellent, glad I could help!

Hello, I am getting the same problem, but can not work out with the solution. Can you help me?

Can you provide a bit more information?

Sorry. I just deleted the whole app and restarted with the tutorial again.

Here it is (again):

Template-loader postmortem Django tried loading these templates, in this order:

Using engine django:

django.template.loaders.app_directories.Loader: /home/khizirsiddiqui/my-first-blog/myvenv/lib/python3.5/site-packages/django/contrib/admin/templates/blog\post_list.html

(Source does not exist) django.template.loaders.app_directories.Loader: /home/khizirsiddiqui/my-first-blog/myvenv/lib/python3.5/site-packages/django/contrib/auth/templates/blog\post_list.html (Source does not exist)

But my template is in my-first-blog/blog/.

That's a strange path. Could one of your settings.py entries for the template config be missing a comma between two strings in a list?

It is working on my Windows

PythonAnywhere is not running on Windows. Your paths need to be valid Linux paths and a backslash is not valid (Linux uses forward slashes as path separators). My guess is you've got a Windows-y path in your settings somewhere.

How do I search that 'Windows-y'?

Thanks, it is now working with the forward slash

Excellent, thanks for confirming!

How should I put the path for my templates in settings.py, should it must be a direct path or the virtualenv path. Could you please give an example.

the direct path, not the virtualenv path.

99% of the time you shouldn't need to add any template paths to settings.py. if you just keep your templates in a folder called "templates" inside one of your own apps (and it's in INSTALLED_APPS), then django will find it automatically.