django加载模板文件

          views.py中编写相关代码

  • 第一种加载方式
from django.http import HttpResponse
from django.template import loader,Context

def index(req):
#生成模板对象
t=loader.get_template("index.html")
#生成Context对象
context = Context({"title":"hello world","name":"JohnRey"})
    return HttpResponse(t.render(context));
  • 第二种加载方式
from django.shortcuts import render_to_response
def index(req):
return render_to_response("index.html",{"title":"hello world","name":"JohnRey"})
posted @ 2016-04-23 18:05  JohnRey  阅读(295)  评论(0编辑  收藏  举报