问题
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 :(
回答1:
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',
]
回答2:
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/',
)
回答3:
If anyone is trying this with Django 1.7 and Python 3 or higher, I did this:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'mysite_dir/templates')]
where myfile_dir can be any dir under mysite. File structure would be something like this:
manage.py
mysite/
mysite/
mysite_dir/
templates/
____init__.py
models.py
#...
another_app/
This answer may seem repetitive but I know I always like to see things from a more recent date... so hope this helps
回答4:
I struggled with a similar issue for a good several hours and found that I could get the template to load if I put it in a /templates subdirectory within the app using the template. You don't even need to set TEMPLATE_DIRS in settings.py, the file is found automatically. Unfortunately the TemplateDoesNotExist exception is not very descriptive and in some cases literally wrong. If you see in the Template-loader postmortem something similar to the following:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/Users/me/mysite/myapp/index.html
And the file path looks correct for the template you're trying to load, it's likely that the file exists but it's not accessible. Try moving it to a /templates subdirectory of the app.
回答5:
If you follow Django's documentation, they recommend namespacing your templates, where under templates, you have /yourapp/ again before the templates. See here.
So your structure might look like:
Project Folder
--Project
--yourapp
--templates
--yourapp
--layout.html
But what they don't make clear is that any hardcoded references to those templates (for example, the render function), need to also prepend your app name to those as well! I thought Django was doing a recursive search for us, but I gave it too much credit. You need to point to the exact link.
e.g.
return render(request, "login.html")
goes to
return render(request, "orders/login.html")
You'll likely need this to in any extends statements or where you use static to link to files.
回答6:
just set DEBUG to false in your Django settings file
来源:https://stackoverflow.com/questions/14150341/django-the-templatedoesnotexist-error