Django学习系列之五:Django 的模板的render替换render_to_string用法及templates模板查找路径顺序

Django学习系列之五:Django 的模板的render替换render_to_string用法及templates模板查找路径顺序

1.Django 的模板的render替换render_to_string

from djangon.shortcut import reder
#from django.template.loader import render_to_string
#from django.http import HttpResponse
def  index(request):
	#html = render_to _string('index.html')
	#return HttpResponse(html)
	Retrn render(request,'index.html')

2.templates模板查找路径顺序
注意:要使用templates一定要在setting.py设置模板的默认路径
setting.py

Templates  下的DIR
'DIRS':[os.path.join(BASE_DIR, 'templates')]
#BaseDir代表当前项目的路径
'APP_DIRS': True,
#app_dirs默认为ture
#app_dirs如果为ture,Django除了会在默认的templates路径查找,还会在会在默认的路径找不到templates的时候会去自己的app查找templates文件,如果自己的app找不到就会在已经安装的app里面查找!
#注意:templates文件名是固定的,另要在install_apps下面添加

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/sghc1429952041/article/details/83240131