Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django & the "TemplateDoesNotExist" error

Ive got an as it seems common beginners problem.

im working on my first django project and when I set up my view I get an "TemplateDoesNotExist" error. Ive spend lots of hours on this now - and I know theres lots of topics on it but nothing helped me till now.

I hope I can supply all the information needed so an advanced django user can probably directly see what Im doing wrong.

im using the developement server. and windows 7 & sqlite3.

this is the error I get:

TemplateDoesNotExist at /skates/
allsk8s.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/skates/
Django Version: 1.4.3
Exception Type: TemplateDoesNotExist

in settings.py I set up the TEMPLATE_DIRS like this:

TEMPLATE_DIRS = (
    r'H:/netz2/skateprojekt/templates/',
)

the template loaders looks like this:

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

this is my view:

from django.shortcuts import render_to_response
from django.template import RequestContext
from sk8.models import Sk8

def AllSk8s(request):
    skates      = Sk8.objects.all().order_by('name')
    context     = {'skates':skates}
    return render_to_response('allsk8s.html', context, context_instance=RequestContext(request))

it should link to allsk8s.html - and it looks like it does but the file can not be found although it is definitely in the right folder. but as you can see:

Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
H:\netz2\skateprojekt\templates\allsk8s.html (File does not exist)

this is a part of my urls.py

    urlpatterns = patterns('',
         url(r'^admin/', include(admin.site.urls)),
         (r'^skates/$', 'sk8.views.AllSk8s'),
 )

and this is the system path:

H:\netz2\skateproject\templates

and in the templates folder is a file called allsk8s.html so as far as I understood it - this should work. I really hope somebody can help me cause this is the second time I ran into a problem like this and I can not figure out the problem.

thanks in advance danielll


edit:

I tried to add this to my settings.py:

import os
DIRNAME = os.path.abspath(os.path.dirname(__file__))

and changed my TEMPLATE_DIRS to:

TEMPLATE_DIRS = (
    os.path.join(DIRNAME, r'H:/netz2/skateprojekt/templates/'),
)

cause I read it would help - but it still returned the same error - so I changed it back again. ;(


edit:

also, Ive checked, when I enter a wront url, it throws this error:

Using the URLconf defined in skateproject.urls, Django tried these URL patterns, in this order:
^admin/
^skates/$

so the skates url should be there - but cant be "resolved" - i dont get it :(


edit:

I found out something new today, the Template-loader postmortem says it also checks these directories:

Using loader django.template.loaders.app_directories.Loader:
C:\Python27\lib\site-packages\django\contrib\auth\templates\allsk8s.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\admin\templates\allsk8s.html (File does not exist)

so I moved my template files there and received a new error - got this fixed by converting my html files from ansi to utf8 and tada - it worked. unfortunately I can not let the template files in this folder cause its not part of the project. when i moved the files back to the original location I was back at the old error :(

like image 276
Daniel Prell Avatar asked Jan 04 '13 02:01

Daniel Prell


People also ask

What is Django used for?

What is Django? Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Built by experienced developers, Django takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.

Was Django a true story?

The wildly popular Django Unchained is the most talked about film of the last month, and aside from the controversy, it's popular because of how badass Django is. However, nobody knew about the real Django–a man named Bass Reeves–whom became a Deputy U.S. Marshal in 1875 at the age of 38.

Is Python and Django are same?

Python has a set of language and object-oriented approach that helps programmers create clear and logical code. Django is a web development python framework that makes it easy to build powerful web applications. The platform comes with useful built-in features found at the Django Admin Interface.

Is Django backend or frontend?

Django is easy to learn and very easy to handle, if you know python then learn Django and if you don't know python then learn python first and then Django, but learn Django anyhow because it is one of the most popular and best backend frameworks.


2 Answers

One of the solution to this problem is you should add apps to in settings.py. I assume you have an application named such as invoice then solution to this is

INSTALLED_APPS = [
'invoice.apps.InoviceConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
like image 79
Harun ERGUL Avatar answered Oct 13 '22 23:10

Harun ERGUL


Holy mother of god! I solved it!

I do not know why - but this is the solution to the "TemplateDoesNotExist" error (in my case).

My folder structure is like this:

netz2 > skateproject

till now i had the templates folder in skateproject and in settings.py i pointed to this directory. this threw the template does not exist error when I tried to open the page in firefox.

as skateproject is the project folder in there ive got an folder sk8 - which is the app that im currently working on and that im trying to execute. The solution is super simple.

I had to move the templates in the subdirectory of the app. which looks like this

netz2 > skateproject > sk8 > templates

and now it works!

So if you have the same problem, make sure your templates folder is not in the root of the project but is a subdirectory of the app youre working on - AND add this path to the settings.py Template_dirs

it looks like this in my example:

TEMPLATE_DIRS = (
    r'H:/netz2/skateprojekt/sk8/templates/',
)
like image 26
Daniel Prell Avatar answered Oct 13 '22 21:10

Daniel Prell