Opened 5 years ago

Closed 5 years ago

Last modified 4 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: UI/UX:

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 Changed 5 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 5 years ago by lrekucki

  • Resolution set to wontfix
  • Status changed from new to closed

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 Changed 5 years ago by anonymous

Thank you.

comment:4 Changed 4 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

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