Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django模板不存在错误,尽管它显示';文件存在';_Python_Django_Django Templates - Fatal编程技术网

Python Django模板不存在错误,尽管它显示';文件存在';

Python Django模板不存在错误,尽管它显示';文件存在';,python,django,django-templates,Python,Django,Django Templates,我无法在Django 1.7中呈现任何html页面。我的“index.html”位于“project/seatalloc/templates/index.html”中,而project/seatalloc/views.py中的My view.py类似于: def index(request): return render(request, 'index.html', dirs=('templates',)) project/project/settings.py已设置模板目录

我无法在Django 1.7中呈现任何html页面。我的“index.html”位于“
project/seatalloc/templates/index.html
”中,而
project/seatalloc/views.py
中的My view.py类似于:

 def index(request):
       return render(request, 'index.html', dirs=('templates',)) 
project/project/settings.py已设置模板目录:

TEMPLATE_DIRS = (
    '/Users/Palak/Desktop/academics/sem3/cs251/lab11/project/seatalloc/templates',

)
URL.py:

urlpatterns = patterns('',
    url(r'^seatalloc/', include('seatalloc.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

我试图严格遵循文档,但不知道Django是否检测到该文件,为什么我在/seatalloc/error处获取TemplateDoesNotExist。我是Django的新手,有人能帮忙吗。

试试这个

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
     os.path.join(BASE_DIR, 'templates/'),
)
试着这样,

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
     os.path.join(BASE_DIR, 'templates/'),
)

首先,不要在settings.py中的模板dirs中使用静态路径(固定路径),请使用:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

TEMPLATE_DIRS = (
      BASE_DIR +'/Templates',


  )
模板目录应该在项目目录中,即manage.py文件所在的目录中

在view.py中,使用
render\u to\u response
,而不仅仅是
render

return  render_to_response("index.html") 

首先,不要在settings.py中的模板dirs中使用静态路径(固定路径),请使用:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

TEMPLATE_DIRS = (
      BASE_DIR +'/Templates',


  )
模板目录应该在项目目录中,即manage.py文件所在的目录中

在view.py中,使用
render\u to\u response
,而不仅仅是
render

return  render_to_response("index.html") 

如果-在您的案例中-您收到TemplateDoesNotExist错误,并且调试页面在所讨论的模板旁边声明“文件存在”,这通常(总是?)意味着该模板引用了另一个找不到的模板


在您的例子中,index.html包含一条语句(
{%extends%},{%include%},
)引用Django找不到的另一个模板。不幸的是,从Django 1.8.3开始,调试页面总是命名基本模板,而不是Django找不到的模板。

如果您遇到TemplateDoesNotExister错误,并且调试页面在相关模板旁边声明“文件存在”,这通常(总是?)意味着该模板引用了另一个找不到的模板


在您的例子中,index.html包含一条语句(
{%extends%},{%include%},
)引用Django找不到的另一个模板。不幸的是,从Django 1.8.3开始,调试页面总是命名基本模板,而不是Django找不到的模板。

我应该在哪里定义PROJECT\u ROOT。在settings.py中定义它会给我提供无效的syntaxDeprecated,因为1.8:@Wtower:你是对的,它已被弃用,试图让它工作浪费了半个小时。我应该在哪里定义PROJECT\u ROOT。在settings.py中定义它会给我提供无效的syntaxDeprecated,因为1.8:@Wtower:你是对的,它已经被弃用了,为了让它工作浪费了半个小时。谢谢!实际上,我从settings.py中删除了整个TEMPLATE_DIRS(..)代码。问题在于模板目录的路径。将其放入project dir后,一切正常。嘿,您建议的对index.html页面有效,例如:但对于任何其他页面或它显示404错误。所有的.html文件都在同一个目录-1中,用于推荐较旧的render\u to\u响应o而不是较新的render。谢谢!实际上,我从settings.py中删除了整个TEMPLATE_DIRS(..)代码。问题在于模板目录的路径。将其放入project dir后,一切正常。嘿,您建议的对index.html页面有效,例如:但对于任何其他页面或它显示404错误。所有的.html文件都在同一个目录-1中,用于推荐一个较旧的渲染响应,而不是较新的渲染。看起来
setalloc
是一个应用程序。实际上,你不需要告诉
template\u dirs
它的模板,正如你在突出显示的那行下面看到的,它已经被应用程序目录加载程序找到了
template_dirs
用于非应用程序模板,通常位于项目根目录中,而不是应用程序中。我解决了问题:我之前提供的url是url(r'^$,views.index,name='index'),我将其更改为url(r'^index$,views.index,name='index'),这+将模板_dir移动到项目根目录解决了问题。感谢大家的帮助看起来像是一个应用程序。实际上,你不需要告诉
template\u dirs
它的模板,正如你在突出显示的那行下面看到的,它已经被应用程序目录加载程序找到了
template_dirs
用于非应用程序模板,通常位于项目根目录中,而不是应用程序中。我解决了问题:我之前提供的url是url(r'^$,views.index,name='index'),我将其更改为url(r'^index$,views.index,name='index'),这+将模板_dir移动到项目根目录解决了问题。谢谢大家的帮助+1,这对我很有帮助。“TemplateDoesNotExist错误”出现在index.html上,该index.html已存在,但被base.html扩展,其中包含缺少的模板。有趣的是,“TemplateDoesNotExist错误”直到我登录Admin并尝试查看站点后才出现,因为缺少的include与登录相关。这就是问题所在!谢谢你,这对我很有帮助。“TemplateDoesNotExist错误”出现在index.html上,该index.html已存在,但被base.html扩展,其中包含缺少的模板。有趣的是,“TemplateDoesNotExist错误”直到我登录Admin并尝试查看站点后才出现,因为缺少的include与登录相关。这就是问题所在!谢谢