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

Ask A Question

Stats

  • Questions 319k
  • Answers 319k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I couldn't find a reference to back this up but… May 14, 2026 at 12:15 am
  • Editorial Team
    Editorial Team added an answer Please have a look at my post @How to use… May 14, 2026 at 12:15 am
  • Editorial Team
    Editorial Team added an answer All what's done in JavaScript won't work. Some users disable… May 14, 2026 at 12:15 am

Related Questions

I'm sorry if the title is confusing but I couldn't think of anything similar
When I pass 'this' to an anonymous function like so: MyClass.prototype.trigger = function(){ window.setTimeout(function(){this.onTimeout();},1000);
Im trying my first form validation with PHP. I need some guidance with the
Sorry for being the 100000th person to ask the same question. But I guess
Edit: When I say SQL Server, I'm really talking about the Management Studio. Sorry

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.