Django学习3-模板中使用变量
2017-04-22 16:10
519 查看
Django可以动态向模板中传递变量,主要是以字典方式传递的,继续前面的test01工程。
首先,在模板中使用变量,是用{{变量名}},这样的方式使用的,传递是在views.py的处理函数return render_to_response('index.html',{})中,通过第二个参数传递的,这是上次的模板,没有使用变量,所以是一个空字典。
比如,模板改为:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Cotent-Type" content="text/html;charset=UTF-8"/>
<title>{{title}}</title>
</head>
<body>
<h1>holle world!</h1>
</body>
</html>
这里,title就是使用的变量,那么,在传递的时候,就要这样写return render_to_response('index.html',{'title':'mytest'})。
除此之外,这里还支持其它类型的变量作参数,包括字典,列表,元组,甚至是类对象,只是在使用的时候,写法不一样,比如,
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render_to_response
class Student():
def __init__(self,score):
self.score=score
def getScore(self):
return self.score
# Create your views here.
def index(req):
# return HttpResponse('<h1>holle world</h1>')
book=('python','c','java')
student=Student(99)
user={'name':'dyp','age':23,'sex':'male'}
return render_to_response('index.html',\
{'title':'mytest3','book':book,'user':user,'student':student})
这里我定义了一个类,一个元组,一个列表,并将它们作为参数传递进去,那么在模板中,我们就可以这样调用<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Cotent-Type" content="text/html;charset=UTF-8"/>
<title>{{title}}</title>
</head>
<body>
<h1>{{book.0}}</h1>
<h1>hello {{user.name}}</h1>
<h1>{{student.score}}</h1>
<h1>{{student.getScore}}</h1>
</body>
</html>
即通过变量名.的方式调用,对于列表和元组,.后跟下标,字典.后跟键,而对于类对象,可以访问它的成员变量和不带参数的成员函数。
好了,主要的就是知道传递的时候以字典传,调用的时候{{}}调用,就这样了,在前面的工程里试试吧。
首先,在模板中使用变量,是用{{变量名}},这样的方式使用的,传递是在views.py的处理函数return render_to_response('index.html',{})中,通过第二个参数传递的,这是上次的模板,没有使用变量,所以是一个空字典。
比如,模板改为:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Cotent-Type" content="text/html;charset=UTF-8"/>
<title>{{title}}</title>
</head>
<body>
<h1>holle world!</h1>
</body>
</html>
这里,title就是使用的变量,那么,在传递的时候,就要这样写return render_to_response('index.html',{'title':'mytest'})。
除此之外,这里还支持其它类型的变量作参数,包括字典,列表,元组,甚至是类对象,只是在使用的时候,写法不一样,比如,
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render_to_response
class Student():
def __init__(self,score):
self.score=score
def getScore(self):
return self.score
# Create your views here.
def index(req):
# return HttpResponse('<h1>holle world</h1>')
book=('python','c','java')
student=Student(99)
user={'name':'dyp','age':23,'sex':'male'}
return render_to_response('index.html',\
{'title':'mytest3','book':book,'user':user,'student':student})
这里我定义了一个类,一个元组,一个列表,并将它们作为参数传递进去,那么在模板中,我们就可以这样调用<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Cotent-Type" content="text/html;charset=UTF-8"/>
<title>{{title}}</title>
</head>
<body>
<h1>{{book.0}}</h1>
<h1>hello {{user.name}}</h1>
<h1>{{student.score}}</h1>
<h1>{{student.getScore}}</h1>
</body>
</html>
即通过变量名.的方式调用,对于列表和元组,.后跟下标,字典.后跟键,而对于类对象,可以访问它的成员变量和不带参数的成员函数。
好了,主要的就是知道传递的时候以字典传,调用的时候{{}}调用,就这样了,在前面的工程里试试吧。
相关文章推荐
- The Definitive Guide To Django 2 学习笔记(六) 第四章 模板 (二)使用模板系统
- Django模板和变量的使用
- Django学习之模板标签和变量
- Django 模板中 变量 过滤器 标签 的使用方法
- The Definitive Guide To Django 2 学习笔记(七) 第四章 模板 (三)使用模板系统
- Django 模板中变量、过滤器、标签的使用方法
- 解决Django模板无法使用perms变量问题的方法
- Django学习笔记9-使用模板
- Django 模板中变量、过滤器、标签的使用方法
- Django学习笔记(1) - 模板的使用
- Django模板标签中使用js变量
- Django模板无法使用perms变量问题解决办法详解
- Django 模板中 变量 过滤器 标签 的使用方法
- django模板中使用加法,以及变量赋值
- django 学习-5 模板使用流程
- Django学习笔记(1) -- Settings全局变量使用
- Django 模板中 变量 过滤器 标签 的使用方法
- python web框架Django学习(五)模板中的循环,条件判断,常用的标签,过滤器的使用
- Django学习日记 第二天 mysql配置 和 模板的使用
- Django学习2-使用模板