Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#15265 closed (wontfix)

Problem with "__call__" method in Templates

Reported by: Stephane Owned by: nobody
Component: Template system Version: 1.3-beta
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,

We have another problem in templates since Django 1.3 when we put dict values into template with "getitem".

We create this class for example:

class MyDict(object):

TEST = {"var": "test", "meuh": "test2"}

def getitem(self, key):

"""
Retrieves a value. Raises a :exc:KeyError if *key* does not exists.
"""
return MyDict.TEST[key]

def call(self, kwargs):

"""
Puts one or more values into this...
"""
for key, value in kwargs.items():

self[key] = value

We add this in a template context:

test = MyDict()
return test

In the template, we want to get the value of "var" key for example:

{{mydict.var}}

No output... But if we remove "call" method, we have the value: "test" (same as before 1.3).

Best regards,

Stephane

Change History (4)

comment:1 by anonymous, 13 years ago

Sorry, underscores are removed...

class MyDict(object):
    TEST = {"var": "test", "meuh": "test2"}

    def __getitem__(self, key):
        """
        Retrieves a value. Raises a :exc:`KeyError` if *key* does not exists.
        """
        return MyDict.TEST[key]

    def __call__(self, **kwargs):
        """
        Puts one or more values into this...
        """
        for key, value in kwargs.items():
            self[key] = value

comment:2 by Łukasz Rekucki, 13 years ago

Resolution: wontfix
Status: newclosed

This is most likely a "won't fix". See ticket #15025 and the docs for render. Citing:

If any part of the variable is callable, the template system will try calling it.

Changed in Django Development version: Previously, only variables that originated with an attribute lookup would be called by the template system. This change was made for consistency across lookup types.

comment:3 by anonymous, 13 years ago

Thank you.

comment:4 by Jacob, 12 years ago

milestone: 1.3

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top