Django中template导入文件

template文件的导入
	template文件就是普通的文件,需要用于template.render()函数渲染,可以有以下
	几种方法来加载文件
	1.用python自带的open()函数,如:
		
		from django.template import Template, Context
		from django.http import HttpResponse
		import datetime

		def current_datetime(request):
			now = datetime.datetime.now()
			# Simple way of using templates from the filesystem.
			# This is BAD because it doesn't account for missing files!
			fp = open('/home/djangouser/templates/mytemplate.html')
			t = Template(fp.read())
			fp.close()
			html = t.render(Context({'current_date': now}))
			return HttpResponse(html)
		这种方法有点费事儿,需要打开和关闭文件
	2.用template的加载api
		在项目的settings.py文件中,找到TEMPLATE_DIRS,然后添加近你的template的目录
		如:

			TEMPLATE_DIRS = (
							'/home/django/mysite/templates',
			)
		注意,后面有一个逗号,这个是python的元组中特别规定的
		这中方法有点古板
	3.将template文件创建在你的项目下,这样查找就比较 方便了,可以这样:
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值