[Django]-Django template context function without running automatically

4👍

I think You shouldn’t mix logic of application with template (the view in MVC pattern). This breaks consistency the architecture. You can call get_messages in views that need it and simply pass messages to the template context, in the others just pass None.

But answering Your question: You can make a proxy object. E.g:

class Proxy(object):
    def __init__(self, request)
        self.request = request
        super(Proxy, self).__init__()

    def get_messages(self):
        # so some expensive things
        return 'string'

# context processor
def context_processor(request):
    return {'messages':Proxy(request)}

# in the view
{{ messages.get_messages }}

You can make this ever more generic, and create Proxy class that has one method (e.g get), and takes one parameter in constructor: a function which takes request object as first parameter. This way You gain generic method to proxy a function call in Your templates. Here it is:

class Proxy(object):
    def __init__(self, request, function)
        self.request = request
        self.function = function
        super(Proxy, self).__init__()

    def get(self):
        return self.function(self.request)

then You can write even cooler than I had written before:

# context processor
def context_processor(request):
    return {'messages':Proxy(request, get_messages)}

# sounds nice to me
{{ messages.get }}
👤Dawid

Leave a comment