小编mit*_*ell的帖子

简单的 hello world 项目中 django 中的 TemplateDoesNotExist 错误

我在 django 中使用模板时遇到问题。我相信我的模板位于正确的位置,并且在查看错误日志中的路径后,该文件就在那里。我也让它在不使用的情况下工作render()。但我已经尝试了多种方法,并且在错误日志中它显示了一个路径,我可以按照该路径到达我尝试渲染的 html 文件。

这是我的项目的文件结构(注意:该文件结构实际上位于 C:\my_website 内部)

在此输入图像描述

相关代码:
下面是我创建的 hello 应用程序的视图。目前的情况是有错误,但如果我注释掉调用并使用未注释的代码,它会显示index.html C:\my_website\my_website\hello\viewsrender()的内容

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
import os
import logging #MS ADDED
logger = logging.getLogger(__name__)#MS ADDED


def index(request):
    #!!! It works with this stuff uncommented
    #index_path =  os.path.join(os.path.dirname(os.path.dirname(__file__)),'hello\\templates\\hello\\index.html')
    #logger.error('Views - index_path is ' + index_path) # MS ADDED
    #with open(index_path) as f:
    #   html_string = f.read()
    #return HttpResponse(html_string)
    return render(request, 'hello/index.hmtl')

Run Code Online (Sandbox Code Playgroud)

以下是我在设置中更改的所有内容。我在 Django 文档中读到,如果您没有指定路径,它将在您创建的应用程序的模板目录中查找(你好)。但是在 TEMPLATES 中将 …

python django django-templates

0
推荐指数
1
解决办法
398
查看次数

标签 统计

django ×1

django-templates ×1

python ×1