使用Jinja2 grouby过滤器的属性错误 [英] Attribute error using Jinja2 grouby filter
问题描述
{%购买年份,year_purchases | groupby('PurchaseDate.year')| reverse% }
< h2 class =year> {{year}}< / h2>
{%for ...%}
{%endfor%}
{%endfor%}
其中purchase是PurchaseEntity的列表:
class PurchaseEntity:
def __init__ (self):
self.PurchaseDate =''
self.Orders = []
def load_from_query(self,result):
self.PurchaseDate = result。 PurchaseDate
出现以下错误:
AttributeError
AttributeError:PurchaseEntity实例没有属性'__getitem__'
问题似乎在environment.py中:
def getitem(self,obj,argument):
返回obj [参数]
除了(TypeError,LookupError):#
获取对象的项目或属性, < - 如果是isinstance(argument,basestring),则会引发错误
:#< - 它永远不会到达
尝试:
attr = str(参数)
除了例外:
传递
else:
try:
返回getattr(obj,attr)
除了AttributeError:
传递
返回self.undefined(obj = obj,name = argument)
我不认为它是一个Jinja2或groupby错误,因为getitem funcion在任何地方都被使用。我GOOGLE了它,并没有任何相关的东西。然而,我所做的是改变除(TypeError,LookupError):行和它的任何替代方案:
除了:
除了例外:
我不知道我的班级错误如果我只是失去了一些东西,因为我尝试了其他类(由SQLAlchemy与自动加载创建),它工作正常。任何建议吗?
btw,发送到getitem的参数是:
$ p $ >>> (obj,argument)
(< project.logic.purchase.PurchaseEntity instance at 0x050DF148> ;,'PurchaseDate')
>>> obj.PurchaseDate
datetime.datetime(2012,9,25,17,1,44)
$ p $
类PurchaseEntity(object):
当您尝试查找某个项目并且没有它时,不扩展任何内容的基本类将抛出AttributeError 。对象抛出TypeError。
>>> class Foo:
... def foo(self):
... void
...
>>> class bar(object):
... def bar(self):
... void
...
>>> foo = Foo()
>>> bar = Bar()
>>> foo ['foo']
Traceback(最近一次调用的最后一个):
在< module>中,第1行的文件< stdin>
AttributeError:Foo实例没有属性'__getitem__'
>>> bar ['bar']
Traceback(最近一次调用的最后一个):
在< module>文件中的< stdin>
TypeError:'Bar'对象没有属性'__getitem__'
Trying to get the groupby filter to work I'm getting an Atribute error. My groupby code looks like:
{% for year, year_purchases in purchases|groupby('PurchaseDate.year')|reverse %}
<h2 class="year">{{ year }}</h2>
{% for ... %}
{% endfor %}
{% endfor %}
Where purchases is a list of PurchaseEntity:
class PurchaseEntity:
def __init__(self):
self.PurchaseDate = ''
self.Orders = []
def load_from_query(self,result):
self.PurchaseDate = result.PurchaseDate
I'm getting the following error:
AttributeError
AttributeError: PurchaseEntity instance has no attribute '__getitem__'
The problem seems to be in environment.py:
def getitem(self, obj, argument):
"""Get an item or attribute of an object but prefer the item."""
try:
return obj[argument]
except (TypeError, LookupError): #<-- Here it raises the error
if isinstance(argument, basestring): #<-- It never reaches here
try:
attr = str(argument)
except Exception:
pass
else:
try:
return getattr(obj, attr)
except AttributeError:
pass
return self.undefined(obj=obj, name=argument)
I don't think that its a Jinja2 or "groupby" error, because the getitem funcion is used everywhere. I googled it and couldn't fint anything related. However, what I did is change the "except (TypeError, LookupError):" line and It worked with any this alternatives:
except:
except Exception:
I don't know if my class delcaration is wrong or if I'm just missing something, because I tried with other clases (created by SQLAlchemy with autoload) and it worked fine. Any suggestions?
Btw, the arguments sent to getitem are:
>>> (obj,argument)
(<project.logic.purchase.PurchaseEntity instance at 0x050DF148>, 'PurchaseDate')
>>> obj.PurchaseDate
datetime.datetime(2012, 9, 25, 17, 1, 44)
Try making PurchaseEntity extend object:
class PurchaseEntity(object):
Basic classes that do not extend anything throw an AttributeError when you try to lookup an item and they do not have it. Objects throw TypeError.
>>> class Foo:
... def foo(self):
... void
...
>>> class Bar(object):
... def bar(self):
... void
...
>>> foo = Foo()
>>> bar = Bar()
>>> foo['foo']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no attribute '__getitem__'
>>> bar['bar']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'Bar' object has no attribute '__getitem__'
这篇关于使用Jinja2 grouby过滤器的属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!