在 <app>/views.py中 添加代码:
方式1:
from django.http import HttpResponse
from django.template.loader import get_template
import datetime
template = get_template('index.html')
html = template.render({'time':now})
return HttpResponse(html)
方式2:
from django.http import HttpResponse
from django.template import Template,Context
import datetime
fp = open('/home/jan/work/py/django-master/mysite/renders/templates/index.html')
template = Template(fp.read())
fp.close()
html = template.render(Context({'time':now}))
return HttpResponse(html)
其中:
template.render({'time':now})#参数接收的是一个字典
template.render(Context({'time':now})) #参数接收的是一个字符串
本文介绍了两种在Django框架中使用不同方法进行模板渲染的具体实现过程。第一种方法利用了get_template函数结合render方法来填充模板变量;第二种方法则直接通过读取文件创建Template对象并使用Context对象来渲染。
920

被折叠的 条评论
为什么被折叠?



