django2中报错TemplateDoesNotExist

在view.py中的代码如下:

from django.shortcuts import render

def index(request):
return render(request,‘pizzas/index.html’)

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

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

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

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

所以修改代码:

from django.shortcuts import render

def index(request):
return render(request,‘pizzas/index.htm’)

成功 !

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

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

2.文件夹templates拼写错误

原文:https://blog.csdn.net/cigo_2018/article/details/81040208

猜你喜欢

转载自blog.csdn.net/weixin_41571948/article/details/87297818