-
模板的组成
组成:Html+罗技控制代码
-
逻辑代控制代码的组成
变量:{{var}}
- render渲染网页的原理是利用Template 与Context
python manage.py shell
>>> from django.template import Context,Template
>>> t= Template("My name is {{name}}")
>>> c=Context({"name":"lian"})
>>> t.render(c)
'My name is lian'
>>>
from django.shortcuts import render
# Create your views here.
import time,datetime
from django.shortcuts import render,HttpResponse
from django.template import Template,Context
def show_time(request):
t1= datetime.datetime.now()
t = Template("<html><body>It is now {{t1}}</body></html>")
c=Context({"t1":t1})
html=t.render(c)
# return HttpResponse('<html><body>It is now %s</body></html>'%t)
return HttpResponse(html)
-
可传对象
from django.shortcuts import render
# Create your views here.
import time,datetime
from django.shortcuts import render,HttpResponse
from django.template import Template,Context
class Animal(object):
def __init__(self,name,sex):
self.name = name
self.sex = sex
def query(request):
l = ['lian','20']
c = Animal('公',"母")
return render(request,"index.html",{'action':locals()})
-
对象数据获取方法---通过.来获取数据,比如数组就array.0,array.1 ,字典就是dic.key
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello {{ action }}</h1>
<h1>hello {{ action.0 }}</h1>
<h1>hello {{ action.1 }}</h1>
<h1>{{action.c.name}}</h1>
</body>
</html>