Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 628909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:41:15+00:00 2026-05-13T19:41:15+00:00

Sorry or the confusing title! It’s actually a lot simpler than it sounds. I’ve

  • 0

Sorry or the confusing title! It’s actually a lot simpler than it sounds.

I’ve got a function:

def get_messages(request):
    # do something expensive with the request
    return 'string'

I want to be able to call that function from the template, so I’ve strapped in with a context processor:

def context_processor(request):
    return {'messages':get_messages(request)}

So now when I have {{messages}} in my template, string prints out. Great.

The problem is get_messages is quite expensive and isn’t always needed. Less than half the templates need it. Is there a way of passing the function to the template and leave it up to the template if it runs or not?

I tried this already:

def context_processor(request):
    return {'messages':get_messages}

But that just outputs the function description <function get_messages at 0x23e97d0> in the template instead of running it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:41:16+00:00Added an answer on May 13, 2026 at 7:41 pm

    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 }}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the confusing title. Let me explain via code: #include <string> #include <boost\function.hpp>
Sorry for the confusing title.... We are developing an application to be used by
Sorry for the confusing title. I tried to make it less verbose, but... What
sorry for the confusing title, its really hard for me to explain what i
Sorry for the confusing title, but it basically says it all. Here's the structures
Sorry for the confusing title :P I'm sure I should know this but I
Sorry for the somewhat confusing title. Not sure really how to title this. My
Sorry if the title seems confusing, but some examples are in order. Let's say
Sorry if the title is confusing. Here is the query I have Select MONTH(DATE(TIMESTAMP)),
Well, sorry about the confusing title but I'm having a slightly annoying problem with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.