django之创建第4-1个项目-访问dict数据

1、修改index.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>django之创建第四个项目</title>
</head>
<body>
    <h1>hello,{{test.name}}</h1> <!--模板  变量用变量定义-->
    <h1>hello,{{test.sex}}</h1>
    <h1>hello,{{test.where}}</h1>
</body>
</html>

2、修改views.py文件

# Create your views here.
#coding:utf-8
from django.http import HttpResponse

#导入templates文件所需导入库
from django.template import loader,Context

def index(request):

    #加载器,加载模板
    t=loader.get_template("index.html")

    #django之创建第4-1个项目-Dict形式
    user={"name":"xiaodeng","sex":"male","where":"EnShi"}
    c=Context({"test":user})#在这里test位变量,user为变量的值,接收user这个字典数据
    return HttpResponse(t.render(c))

3、百度云盘:http://pan.baidu.com/s/1mi1X8Mk

posted @ 2016-04-04 22:02  Xiao|Deng  阅读(253)  评论(0编辑  收藏  举报