您的位置:首页 > 大数据 > 人工智能

在Django块标签中遇到的Error:Could not parse the remainder:'>' from 'user.age>'

2015-11-14 21:49 549 查看
在学习模板的块标签的使用的第一个例子中,就出现了这个错误。

出错时的代码是这样写的:

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",19,"male")
c=Context({"user":person})
return HttpResponse(t.render(c))


报错的截图如下:



这个错误实在是不太好发现,原来问题在这里:

{%  if user.age> 18  %}  <!--这里我将大于号>和user.age之间没有留空格,因此程序将user.age>看成了一个整体,我也是醉了,其它语言是能够识别的出来的-->


解决方法:将大于号>和user.age之间用空格分隔开来即可。

最后的运行结果如下:



小结

语言之间还是存在一些细微的差别的,注意这些小的差别。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: