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

  • Home
  • SEARCH
  • 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 6215419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:03:09+00:00 2026-05-24T07:03:09+00:00

please take a look at the code below. I am trying to store a

  • 0

please take a look at the code below. I am trying to store a lambda expression in a context variable and retrieve it in my custom template tag. but the variable lookup returns an empty string instead of the lambda that I expected. why? any idea?

thanks

konstantin

>>> c = dict(f = lambda x : 'x=%s' % x)
>>> c['f']
<function <lambda> at 0x02D7FFB0>
>>> template.Variable('f').resolve(c)
''
>>>

  • 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-24T07:03:09+00:00Added an answer on May 24, 2026 at 7:03 am

    Passing lambdas to the template in Django 1.2.4 worked fine, after upgrading my code to Django 1.3, I was bit by the same issue too. I gave up on trying to set alters_data flag and trying to apply the patch in ticket 15791 that adds a do_not_call_in_templates flag too (apparently merged in the dev version). The way I sidestepped the problem until a proper solution is in place was to use a factory function without arguments that returned the lambda instead of passing the lambda to the template.

    def return_a_lambda():
        return lambda x : 'x=%s' % x
    
    c = dict(f=return_a_lambda)
    >>> c['f']
    <function return_l at 0x33bc668>
    template.Variable('f').resolve(c)
    <function <lambda> at 0x33ccaa0>
    

    Django’s template calls all context variables as long as they don’t need an argument, hence return_a_lambda is executed and the template gets the lambda in return.

    https://docs.djangoproject.com/en/dev/ref/templates/api/ under “Rendering a context”

    Update:
    A reusable hack would be a factory function that returns a factory function:

    def encapsulate(func):
        def wrapper():
            return func
        return wrapper
    

    or a shorter version:

    def encapsulate(func):
        return lambda: func
    

    with the final code looking like this:

    c = dict(f=encapsulate(lambda x : 'x=%s' % x))
    

    which is easier to interpret. In my case (https://github.com/rosarior/mayan) I now have to do this some 30 times aprox to get the code running in Django 1.3 :'(

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please take a look at this code: template<class T> class A { class base
Please take a look at this below line of code in JSF <h:inputText id=name
Please take a look at the html listed below and let me know why
Please take a look at my code: #include <windows.h> #include <Sti.h> #include <iostream> #pragma
Please take a look at the code. It shouldn't take long to have a
Please take a look at this XML code: <TableLayout android:id=@+id/tableLayout1 android:layout_width=fill_parent android:layout_height=fill_parent android:padding=10dip android:isScrollContainer=true
Please take a look at this code and run it: I'm getting very strange
Please take a look at this code: class Foo { public $barInstance; public function
Hi please take a look at this code: // if the image url doesn't
Please take a look at my code. The error I get in Visual Studio

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.