开发者

Django template doesn't render a Variable's method

开发者 https://www.devze.com 2023-01-04 19:36 出处:网络
I am obviously victim of some dark magic... Here is a template that I render : context = Context({\'my_cube\': c})

I am obviously victim of some dark magic...

Here is a template that I render :

context = Context({'my_cube': c})
template = Template(
    '{% load cube_templatetags %}'
    '{{ my_cube|inspect }} {{ my_cube.measure }}'
)

Here is the implementation of the inspect filter :

def inspect_object(obj):
    return obj.measure()

Here is what the rendering gives me :

>>> template.render(context)
u'6 None'

Does anyone know why the hell does the {{ my_cube.measure }} is not rendered properly, while obviously the function call is successful ???

NB : the measure function does no magic, no internal state is changed, I tested and it gives the same result each time, I also tested to put the inspect before the {{ cube.measure }}.... doesn't change anything. I have absolutely no clue on what's going on...

EDIT : I know where it seems to be coming from. But it is still strange. For some reason, my object's attribute are not resolved by template.Variable :

>>> Variable('measure').resolve(c) == None
True
>>> Variable('testitesti').resolve(c) == None
True
>>> c.testitesti()
68
#implementation of testitesti :
def testitesti(self):
    re开发者_运维问答turn 68


Well... I found the damn thing !

The object I was trying to render had a __getitem__ method that was just empty, so dictionnary indexing worked on this object (no error thrown), so of course the function call was not made !


Inspect is being registered as a filter, yep? I'm assuming so else the whole template would choke. Is there a possible reserved word clash? inspect is a pretty loaded term, after all. Have you tried renaming that filter to something else?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号