Python系列视频教程: Django【13讲】第三讲 模板变量的使用

31 篇文章 0 订阅
23 篇文章 0 订阅

Python系列视频教程: Django【13讲】第三讲 模板变量的使用
step1:修改index.html

定义模板变量 title和user

模板变量放在两对大括号里面

{{}}

<title>{{title}}</title>
</head>
<body>
<h1>hello {{user}}</h1>
</body>
</html>

step2:修改index函数-使用模板变量title和user(给模板变量填充数据)

  return render_to_response('index.html',{'title':'my page','user':'Tom'})
 http://127.0.0.1:8000/blog/index/

显示

 Hello Tom 

step3:user变量中加入更多的field

def index(req):
  user={'name':'tom','age':23,'sex':'male'}
  return render_to_response('index.html',{'title':'my page','user':user})

  http://127.0.0.1:8000/blog/index/

显示如下:

hello {'age': 23, 'name': 'tom', 'sex': 'male'}

修改index.html

<body>
<h1>hello {{user.name}}</h1>
<li>{{user.age}}</li>
<li>{{user.sex}}</li>

</body>

 显示如下:

hello tom

  • 23
  • male

     

    在html模板里面,可以直接使用数据字典中的数据

    from django.shortcuts import render_to_response


    # Create your views here.

    def index(req):
      user={'name':'tom','age':23,'sex':'male'}
      return render_to_response('index.html',{'title':'my page','user':user})
     

     

    step4:使用类对象里面的数据

    from django.shortcuts import render_to_response
    # Create your views here.
    class Person(object):
      def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex

    def index(req):
      #user={'name':'tom','age':23,'sex':'male'}
      user=Person('Peter',23,'male')
      return render_to_response('index.html',{'title':'my page','user':user})
     

    hello Peter

  • age:23
  • sex:male

    step5:我们再使用list

    def index(req):
      #user={'name':'tom','age':23,'sex':'male'}
      user=Person('Peter',23,'male')
      book_list=['python','java','php','web']
      return render_to_response('index.html',{'title':'my page','user':user,'book_list':book_list})
     

    使用list的索引

    <body>
    <h1>hello {{user.name}}</h1>
    <li>age:{{user.age}}</li>
    <li>sex:{{user.sex}}</li>
    <li>book:{{book_list.0}}</li>
    <li>book:{{book_list.1}}</li>
    <li>book:{{book_list.2}}</li>
    <li>book:{{book_list.3}}</li>

    </body>

    hello Peter

  • age:23
  • sex:male
  • book:python
  • book:java
  • book:php
  • book:web

     

    step6:调用对象的方法

    class Person(object):
      def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex
      def say(self):#没有参数,而且有返回值
        return "I'm "+self.name

    <div>
    the {{user.name}} say:{{user.say}}
    </div>

     

    hello Peter

  • age:23
  • sex:male
    the Peter say:I'm Peter
  • book:python
  • book:java
  • book:php
  • book:web

     

    总结:

    视图向模板传递变量的方式有很多种:

    普通变量

    字典

    对象

    对象的属性

    对象的方法

     

    注意:

    使用对象引用(user.sex)的时候,有个优先级:

      字典》对象的属性》对象的方法》列表

     

     

     

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值