TemplateDoesNotExist(找不到index.html) 问题记录

在view.py中的代码如下:

from django.shortcuts import render

def index(request):

      return render(request,'pizzas/index.html')

在templates中的模板也存在, 可是执行的时候始终报错,找不到文件(index.html)。

TemplateDoesNotExist(找不到index.html) 问题记录


TemplateDoesNotExist(找不到index.html) 问题记录

找问题找了好久,直接把报错路径填写到地址栏,果然打不开,所以就在文件上找问题:

报错路径:C:\Users\420003\python_work\learning_log\pizzas\templates\pizzas\index.html

TemplateDoesNotExist(找不到index.html) 问题记录

终于发现以下,Windows系统把创建的html文件自动定义为'htm',而不是‘html’

TemplateDoesNotExist(找不到index.html) 问题记录

所以修改代码:

from django.shortcuts import render

def index(request):

      return render(request,'pizzas/index.htm')

成功 !

TemplateDoesNotExist(找不到index.html) 问题记录


在查询资料的过程中,发现很多人也遇到这样的报错,但是引发的条件不一样,这里也说两个:

1. 没有在templates下重新建立一个以APP名的子文件夹,这回导致python查询路径冲突

2.拼写错误,将templates少了一个S,或者index文件名写错等等。