在學習模闆的塊标簽的使用的第一個例子中,就出現了這個錯誤。
出錯時的代碼是這樣寫的:
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之間用空格分隔開來即可。
最後的運作結果如下:
小結
語言之間還是存在一些細微的差别的,注意這些小的差别。