django& “TemplateDoesNotExist”错误 [英] django & the "TemplateDoesNotExist" error
问题描述
我在第一个django项目中工作,当我设置我的视图时,我得到一个TemplateDoesNotExist错误。
我现在花了很多时间 - 我知道了很多话题,但迄今没有任何帮助。
我希望我能提供所有所需的信息,所以高级django用户可能会直接看到我做错了什么。
im使用开发服务器。和Windows 7& sqlite3。
这是我得到的错误:
TemplateDoesNotExist at / skate /
allsk8s.html
请求方法:GET
请求URL:http://127.0.0.1:8000/skates/
Django版本:1.4.3
异常类型:TemplateDoesNotExist
在settings.py中,我设置了这样的TEMPLATE_DIRS:
TEMPLATE_DIRS =(
r'H:/ netz2 / skateprojekt / templates /',
)
模板加载器如下所示:
code> TEMPLATE_LOADERS =(
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
#'django.template。 loaders.eggs.Loader',
)
这是我的观点:
从django.shortcuts导入render_to_response从django.template导入RequestContext
从sk8.models导入Sk8
def AllSk8s(reque st):
skates = Sk8.objects.all()。order_by('name')
context = {'skates':skates}
return render_to_response('allsk8s.html',context ,context_instance = RequestContext(request))
它应该链接到allsk8s.html - 它看起来像但是,无法找到文件,尽管它绝对是在正确的文件夹。
,但您可以看到:
模板加载程序postmortem
Django尝试加载这些模板,在此订单:
使用加载器django.template.loaders.filesystem.Loader:
H:\\\
etz2\skateprojekt\templates\allsk8s.html(文件不存在)
这是我的urls.py的一部分
urlpatterns = patterns('',
url(r'^ admin /',include(admin.site.urls)),
(r'^ skates / $' skbview.AllSk8s'),
)
这是系统路径: / p>
H:\\\
etz2\skateproject\templates
并且在templates文件夹中有一个名为allsk8s.html
的文件,据我所知 - 这应该是正常的。
我真的希望有人可以帮助我,因为这是第二次遇到这样的问题,我无法弄清楚这个问题。
谢谢提前
danielll
编辑:
我尝试将其添加到我的settings.py:
import os
DIRNAME = os.path.abspath(os。 path.dirname(__ file__))
并将我的TEMPLATE_DIRS更改为:
TEMPLATE_DIRS =(
os.path.join(DIRNAME,r'H:/ netz2 / skateprojekt / templates /'),
)
因为我读它会有所帮助 - 但它仍然返回相同的错误 - 所以我再次更改。 ;(
编辑:
当我输入一个wront url,它会抛出这个错误:
使用skateproject.urls中定义的URLconf,Django尝试了这些URL模式,按以下顺序:
^ admin /
溜冰鞋/ $
so溜冰鞋的url应该在那里 - 但不能解决 - 我没有得到它:(
编辑: p>
我今天发现了一些新东西,模板加载器postmortem说它也检查这些目录:
使用加载器django.template.loaders.app_directories.Loader:
C:\Python27\lib\site- packages\django\contrib\auth\templates\allsk8s .html(文件不存在)
C:\Python27\lib\site-packages\django\contrib\admin\templates\allsk8s.html(文件不存在)
所以我把模板文件移到那里并接收编辑一个新的错误 - 通过将我的html文件从ansi转换为utf8和tada得到这一点 - 它的工作。
不幸的是我不能让这个文件夹中的模板文件导致它不是项目的一部分。当我将文件移回原来的位置时,我回到了旧的错误:(
神的母亲!解决了它!
我不知道为什么 - 但这是TemplateDoesNotExist错误(在我的例子中)的解决方案。
我的文件夹结构如下:
netz2> skateproject
skateproject中的templates文件夹和settings.py我指向这个目录
这个模板不存在错误,当我试图在firefox中打开该页面。
作为skateproject的项目文件夹,它有一个文件夹sk8 - 这是目前正在处理的应用程序,并且正在尝试执行。
解决方案是超简单的。
我不得不移动应用程序子目录中的模板,看起来像这样
netz2> skateproject> sk8> templates
现在它的作品!
S o如果您有同样的问题,请确保您的模板文件夹不在项目的根目录,但是您正在处理的应用程序的子目录 - 并将此路径添加到settings.py Template_dirs
在我的例子中看起来像这样:
TEMPLATE_DIRS =(
r'H: / netz2 / skateprojekt / sk8 / templates /',
)
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 :(
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/',
)
这篇关于django& “TemplateDoesNotExist”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!