在学习模板的块标签的使用的第一个例子中,就出现了这个错误。
出错时的代码是这样写的:
index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>wuranghao</title>
</head>
<body>
<h1>hello:{{user.name}}</h1>
<h1>age:{{user.age}}</h1>
<h1>male:{{user.sex}}</h1>
{% if user.age> 18 %}
<li>未成年</li>
{% endif %}
</body>
</html>
views.py中的代码如下:即传送给了模板文件一个对象类型
from django.http import HttpResponse
from django.template import loader,Context
class Person(object):
def __init__(self,name,age,sex):
self.name=name
self.age=age
self.sex=sex
def getName(self):
return "my name is "+self.name
def index1(request):
t=loader.get_template("index1.html")
person=Person("wuranghao",,"male")
c=Context({"user":person})
return HttpResponse(t.render(c))
报错的截图如下:
这个错误实在是不太好发现,原来问题在这里:
解决方法:将大于号>和user.age之间用空格分隔开来即可。
最后的运行结果如下:
小结
语言之间还是存在一些细微的差别的,注意这些小的差别。